以编程方式设置工具栏图标颜色

5
如何在Toolbar/AppBarLayout中通过编程方式设置图标(主页和溢出菜单图标)的颜色?
我想为活动中的单个片段更改工具栏的颜色方案。将AppBarLayout的背景设置为浅色(例如使用appBarLayout.setBackgroundResource(..);设置为浅灰色)会导致白色图标和白色标题,这些内容几乎不可见。
它的布局如下:
<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:theme="@style/ToolbarStyle"
        app:layout_scrollFlags="scroll|enterAlways"/>

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

找到解决方法了


你在@style/ToolbarStyle中有什么? - ootinii
3个点的菜单图标被称为溢出图标。 - Gueorgui Obregon
@ootinii 这不应该成为问题 - 我需要在特定的片段中通过代码覆盖那个样式。 - Seb Jachec
@g2o 忘记了这个词 - 谢谢! - Seb Jachec
你想要更改主页图标还是返回图标? - Gueorgui Obregon
显示剩余2条评论
3个回答

10

使用支持23的方法可以轻松更改溢出图标。以下是Lorne Laliberte的答案提供的方法。


public static void setOverflowButtonColor(final Toolbar toolbar, final int color) {
    Drawable drawable = toolbar.getOverflowIcon();
    if(drawable != null) {
        drawable = DrawableCompat.wrap(drawable);
        DrawableCompat.setTint(drawable.mutate(), color);
        toolbar.setOverflowIcon(drawable);
    }
}

你可以通过传递你自定义的可绘制对象来改变你的主页面。

getSupportActionBar().setHomeAsUpIndicator(R.drawable.your_drawable)

或更改其颜色

final Drawable upArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
upArrow.setColorFilter(Color.parseColor("#FFFFFF"), PorterDuff.Mode.SRC_ATOP);
getSupportActionBar().setHomeAsUpIndicator(upArrow);

编辑: 如果您想更改更多元素,请点击此处阅读一篇很好的文章,可以更改所有工具栏图标颜色。

希望这对您有所帮助!


嘿,这真的帮了很多!我使用了你的第一个代码片段来更改溢出按钮的颜色,并使用你链接的文章中的第一个代码片段来更改其他工具栏视图的颜色。我接受了你的答案,因为它非常有帮助,但我也会添加自己的方法,将几个代码片段组合在一起。 - Seb Jachec

6

我已经接受了最有帮助的答案并对其进行了评论),解释了我如何使用它链接的代码片段来形成单个AppBarLayout/Toolbar着色方法。它涵盖了背景、标题、副标题、返回/抽屉图标和溢出图标的颜色,以及任何自定义的ImageButtons。这是我的结果(原谅英语中的'colour' 拼写错误(!)..):

public static void colouriseToolbar(AppBarLayout appBarLayout, @ColorInt int background, @ColorInt int foreground) {
    if (appBarLayout == null) return;

    appBarLayout.setBackgroundColor(background);

    final Toolbar toolbar = (Toolbar)appBarLayout.getChildAt(0);
    if (toolbar == null) return;

    toolbar.setTitleTextColor(foreground);
    toolbar.setSubtitleTextColor(foreground);

    final PorterDuffColorFilter colorFilter
            = new PorterDuffColorFilter(foreground, PorterDuff.Mode.MULTIPLY);

    for (int i = 0; i < toolbar.getChildCount(); i++) {
        final View view = toolbar.getChildAt(i);

        //todo: cal icon?
        Log.d(Globals.TAG, "view: "+i+" "+view.getClass().toString());

        //Back button or drawer open button
        if (view instanceof ImageButton) {
            ((ImageButton)view).getDrawable().setColorFilter(colorFilter);
        }

        if (view instanceof ActionMenuView) {
            for (int j = 0; j < ((ActionMenuView) view).getChildCount(); j++) {

                final View innerView = ((ActionMenuView)view).getChildAt(j);

                //Any ActionMenuViews - icons that are not back button, text or overflow menu
                if (innerView instanceof ActionMenuItemView) {
                    Log.d(Globals.TAG, "view (actionmenuitemviwe): "+i);

                    final Drawable[] drawables = ((ActionMenuItemView)innerView).getCompoundDrawables();
                    for (int k = 0; k < drawables.length; k++) {

                        final Drawable drawable = drawables[k];
                        if (drawable != null) {
                            final int drawableIndex = k;
                            //Set the color filter in separate thread
                            //by adding it to the message queue - won't work otherwise
                            innerView.post(new Runnable() {
                                @Override
                                public void run() {
                                    ((ActionMenuItemView) innerView).getCompoundDrawables()[drawableIndex].setColorFilter(colorFilter);
                                }
                            });
                        }
                    }
                }
            }
        }
    }

    //Overflow icon
    Drawable overflowIcon = toolbar.getOverflowIcon();
    if (overflowIcon != null) {
        overflowIcon.setColorFilter(colorFilter);
        toolbar.setOverflowIcon(overflowIcon);
    }
}

这似乎在Android 10上无法工作,即使修复了,这种方法也很可能在未来出现问题... - Ivan Ičin

1
您可以通过以下代码将home更改为向上箭头图标
Drawable upArrow = ContextCompat.getDrawable(this, R.drawable.back);
getSupportActionBar().setHomeAsUpIndicator(upArrow);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接