首页 文章

ActionBar“向上”按钮颜色变化

提问于
浏览
0

我正在尝试更改我在Dialog上显示的ActionBar上的“up”图标的颜色 .

基本上,我在ActionBar上有蓝色背景 . 一旦我点击搜索图标,我将背景更改为灰色,这是我希望我的向上箭头也是灰色的(在前一种情况下为蓝色背景的白色) .

这是我点击搜索时更改背景的代码:

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;
                }
        });

在上面的例子中,灰色背景按预期应用,但为什么后箭头没有显示?它还是白色的 .

编辑:我注意到的一件事是,它在项目展开时不适用,但在搜索折叠后适用 . 因此,当对话框打开时,它的白色 . 当我点击搜索以展开它时,它仍然是白色的 . 但当我点击它并且搜索崩溃时,它变成了灰色 .

编辑:我在我的 MyActivity 中设置了我的操作栏,如下所示:

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);
}

另外,这是我的 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>

这是我申请上面工具栏的 theme

<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>

3 回答

  • 1

    您可以将 ColorFilter 添加到 Drawable 以对其进行着色:

    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;
                    }
                });
    
  • 1

    在setcontentview下面的oncreate方法中添加以下代码以更改向上箭头的颜色 .

    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);
    
  • 0

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

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

相关问题