简体   繁体   中英

ActionBar “up” button color change

I'm trying to change the color of the "up" icon on the ActionBar that I show on my Dialog.

Basically, I have a blue background on the ActionBar. Once I click the search icon, I change the background to grey and this is when I want my up arrow to be gray too (its white in the previous case with blue background).

Here is the code when I change the background on search clicked:

MenuItemCompat.setOnActionExpandListener(searchView,
new MenuItemCompat.OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionExpand(MenuItem menuItem) {
                    mIsSearchViewOpen = true;
                    ActionBar ab = ((MyActivity) getActivity()).getSupportActionBar();
                    //Change the ActionBar background color when SearchView is expanded
                    if (ab != null) {
                        ab.setDisplayShowHomeEnabled(true);
                        ab.setDisplayHomeAsUpEnabled(true);
                        ab.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_gray_rect));
                        ab.setHomeAsUpIndicator(R.drawable.ic_back_arrow); //this doesn't work. its still white.
                    }
                    return true;
                }
        });

In the above case, the grey background is applied as expected but why is the back arrow not shown? It's still white.

EDIT: one thing I noticed is that it does not apply when the item is expanded, but applies after the search is collapsed. So, when the dialog opens, its white. When I tap on search to expand it, it is still white. But when I hit that and the search collapses, it turns grey.

EDIT: I set my action bar in my MyActivity like follows:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    if(toolbar == null) {
      ActionBar ab = getSupportActionBar();
      if (ab != null) {
          ab.setDisplayHomeAsUpEnabled(true);
          ab.setElevation(0);
        }
    } else {
       setSupportActionBar(toolbar);
}

Also, here is my toolbar :

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay"
    app:elevation="0dp">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:contentInsetLeft="72dp"
        android:contentInsetStart="72dp"
        app:contentInsetLeft="72dp"
        app:contentInsetStart="72dp"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        app:theme="@style/MyApp.ThemeOverlay.AppCompat.ActionBar"
        app:titleTextAppearance="@style/TextAppearance.MyApp.ActionBar.Title"
        tools:ignore="UnusedAttribute">

        <ImageView
            android:id="@+id/toolbar_logo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/logo_action_bar"
            android:visibility="gone"
            tools:ignore="ContentDescription" />

        <Spinner
            android:id="@+id/spinner_nav"
            style="@style/Widget.MyApp.Spinner.DropDown.ActionBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone" />

    </android.support.v7.widget.Toolbar>

</android.support.design.widget.AppBarLayout>

And this is my theme that I apply to my Toolbar above:

<style name="MyApp.ThemeOverlay.AppCompat.ActionBar" parent="Theme.AppCompat">
        <item name="android:homeAsUpIndicator">@drawable/ic_arrow_back</item>
        <item name="homeAsUpIndicator">@drawable/ic_arrow_back</item>
    </style>

You can add a ColorFilter to the Drawable to tint it:

        MenuItemCompat.setOnActionExpandListener(searchView,
            new MenuItemCompat.OnActionExpandListener() {
                @Override
                public boolean onMenuItemActionExpand(MenuItem menuItem) {
                    mIsSearchViewOpen = true;
                    ActionBar ab = ((DSActivity) getActivity()).getSupportActionBar();
                    //Change the ActionBar background color when SearchView is expanded
                    if (ab != null) {
                        ab.setDisplayShowHomeEnabled(true);
                        ab.setDisplayHomeAsUpEnabled(true);
                        ab.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_gray_rect));
                        Drawable drawable = ResourcesCompat.getDrawable(getActivity(), R.drawable.ic_back_arrow, null);
                        drawable.setColorFilter(new PorterDuffColorFilter(getResources().getColor(R.color.gray), PorterDuff.Mode.SRC_IN));
                        ab.setHomeAsUpIndicator(drawable);
                    }
                    return true;
                }
            });

Add below code for changing the color of up arrow in oncreate method below setcontentview.

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    final Drawable upArrow = ContextCompat.getDrawable(this, R.drawable.abc_ic_ab_back_mtrl_am_alpha);
    upArrow.setColorFilter(getResources().getColor(R.color.grey), PorterDuff.Mode.SRC_ATOP);
    getSupportActionBar().setHomeAsUpIndicator(upArrow);

在清单活动声明中,确保按如下所示指定主题:

android:theme="@style/MyApp.ThemeOverlay.AppCompat.ActionBar"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM