【问题标题】:ActionBar "up" button color changeActionBar“向上”按钮颜色变化
【发布时间】:2016-12-06 19:04:55
【问题描述】:

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

基本上,我在 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>

【问题讨论】:

    标签: android android-actionbar


    【解决方案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;
                    }
                });
    

    【讨论】:

    • 菜单搜索项展开时不适用。它在崩溃后应用。知道为什么吗?
    • 如何在这段代码之外设置 ActionBar?
    • 也许你应该添加类似于 onMenuItemActionCollapse() 的东西
    • 我添加了关于如何添加操作栏的代码。我确实有代码 onMenuItemCollapse。但我希望颜色在展开而不是折叠时发生变化。
    • 我建议你确保你在扩展时确实点击了这段代码(调试确定)。如果这可能没有帮助,您可以创建一个自定义 Drawable 并将其设置在主题中。
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 2013-04-27
        • 2012-07-10
        • 1970-01-01
        • 2018-09-28
        • 2020-09-01
        • 1970-01-01
        • 2021-05-29
        • 1970-01-01
        相关资源
        最近更新 更多