Change the actionbar homeAsUpIndicator Programamti

2019-01-24 16:37发布

I used the following hack to change the homeAsupIndicator programmatically.

int upId = Resources.getSystem().getIdentifier("up", "id", "android");
if (upId > 0) {
    ImageView up = (ImageView) findViewById(upId);
up.setImageResource(R.drawable.ic_action_bar_menu);
up.setPadding(0, 0, 20, 0);
}

But this is not working on most new phones (HTC One, Galaxy S3, etc). Is there a way that can be changed uniformly across devices. I need it to be changed only on home screen. Other screens would have the default one. So cannot use the styles.xml

11条回答
Explosion°爆炸
2楼-- · 2019-01-24 16:52

use getActionBar().setCustomView(int yourView); because ActionBar haven't method to change homeUp icon!

查看更多
看我几分像从前
3楼-- · 2019-01-24 16:53

The solution by checking Resources.getSystem() doesn't work on all devices, A better solution to change the homeAsUpIndicator is to set it @null in style and change the logo resource programmatically.

Below is my code from style.xml

<style name="Theme.HomeScreen" parent="AppBaseTheme">
  <item name="displayOptions">showHome|useLogo</item>
  <item name="homeAsUpIndicator">@null</item>
  <item name="android:homeAsUpIndicator">@null</item>
</style>

In code you can change the logo using setLogo() method.

getSupportActionBar().setLogo(R.drawable.abc_ic_ab_back_holo_light); //for ActionBarCompat
getActionBar().setLogo(R.drawable.abc_ic_ab_back_holo_light); //for default actionbar for post 3.0 devices

Also note that the Android API 18 has methods to edit the homeAsUpIndicator programatically, refer documentation.

查看更多
不美不萌又怎样
4楼-- · 2019-01-24 16:54

API 18 has new methods ActionBar.setHomeAsUpIndicator() - unfortunately these aren't supported in the support library at this moment

http://developer.android.com/reference/android/app/ActionBar.html#setHomeAsUpIndicator(android.graphics.drawable.Drawable)

edit: these are now supported by the support library http://developer.android.com/reference/android/support/v7/app/ActionBar.html#setHomeAsUpIndicator(android.graphics.drawable.Drawable)

查看更多
不美不萌又怎样
5楼-- · 2019-01-24 16:58

This is what i did to acheive the behavior. I inherited the base theme and created a new theme to use it as a theme for the specific activity.

<style name="CustomActivityTheme" parent="AppTheme">
    <item name="android:homeAsUpIndicator">@drawable/custom_home_as_up_icon</item>
</style>

and in the android manifest i made the activity theme as the above.

<activity
        android:name="com.example.CustomActivity"
        android:theme="@style/CustomActivityTheme" >
</activity>

works great. Will update again when i check on all devices I have. Thanks @faylon for pointing in the right direction

查看更多
不美不萌又怎样
6楼-- · 2019-01-24 16:59

Adding to Fllo answer Change the actionbar homeAsUpIndicator Programamtically

I was able to use this hack on Android 4+ but could not understand why the up/home indicator was back to the default one when search widget was expanded. Looking at the view hierarchy, turns out that the up/home indicator + icon section of the action bar has 2 implementations and of course the first on is the one for when the search widget is not expanded. So here is the code I used to work around this and get the up/home indicator changed in both cases.

    mSearchItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            // https://stackoverflow.com/questions/17585892/change-the-actionbar-homeasupindicator-programamtically
            int actionBarId = getResources().getIdentifier("android:id/action_bar", null, null);
            View view = getActivity().getWindow().getDecorView().findViewById(actionBarId);
            if (view == null
                    || !(view instanceof ViewGroup)) {
                return true;
            }
            final ViewGroup actionBarView = (ViewGroup)view;

            // The second home view is only inflated after 
            // setOnActionExpandListener() is first called
            actionBarView.post(new Runnable() {
                @Override
                public void run() {
                    //The 2 ActionBarView$HomeView views are always children of the same view group
                    //However, they are not always children of the ActionBarView itself
                    //(depends on OS version)
                    int upId = getResources().getIdentifier("android:id/up", null, null);
                    View upView = actionBarView.findViewById(upId);
                    ViewParent viewParent = upView.getParent();
                    if (viewParent == null) {
                        return;
                    }
                    viewParent = viewParent.getParent();
                    if (viewParent == null
                            || !(viewParent instanceof ViewGroup)) {
                        return;
                    }

                    ViewGroup viewGroup = (ViewGroup) viewParent;
                    int childCount = viewGroup.getChildCount();
                    for (int i = 0; i < childCount; i++) {
                        View childView = viewGroup.getChildAt(i);
                        if (childView instanceof ViewGroup) {
                            ViewGroup homeView = (ViewGroup) childView;
                            upView = homeView.findViewById(upId);
                            if (upView != null
                                    && upView instanceof ImageView) {
                                Drawable upDrawable = getResources().getDrawable(R.drawable.ic_ab_back_holo_dark_am);
                                upDrawable.setColorFilter(accentColorInt, PorterDuff.Mode.MULTIPLY);
                                ((ImageView) upView).setImageDrawable(upDrawable);
                            }
                        }
                    }
                }
            });
查看更多
登录 后发表回答