【问题标题】:Set Toolbar Icon Colour Programmatically以编程方式设置工具栏图标颜色
【发布时间】:2016-07-01 11:09:10
【问题描述】:

如何在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>

Solution found

【问题讨论】:

  • @style/ToolbarStyle 中有什么?
  • 三点菜单图标称为溢出图标
  • @ootinii 没关系 - 我需要通过特定片段中的代码覆盖该样式
  • @g2o 忘了这个词 - 谢谢!
  • 您是要更改主页图标还是返回图标?

标签: android material-design android-toolbar android-appbarlayout


【解决方案1】:

支持 23 可以轻松更改溢出图标。这是来自Lorne Laliberte answer 的方法

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

编辑: 如果您想更改更多元素here 是更改所有工具栏图标颜色的好帖子。

希望这会有所帮助!

【讨论】:

  • 嘿,这帮了大忙!我使用您的第一个代码 sn-p 更改溢出按钮颜色,并使用 the post you linked 的第一个代码 sn-p 更改其他工具栏视图的颜色。我接受了你的回答,因为它很有帮助,但我将添加我自己的方法,该方法也是几段代码的组合。
【解决方案2】:

您可以通过以下代码将home 更改为up icon

Drawable upArrow = ContextCompat.getDrawable(this, R.drawable.back);
getSupportActionBar().setHomeAsUpIndicator(upArrow);

【讨论】:

    【解决方案3】:

    我接受了most helpful answer (and commented on it) 解释说我使用了它的链接代码 sn-ps 的组合来形成一个单一的AppBarLayout/Toolbar 着色方法。它涵盖背景、标题、副标题、后退/抽屉图标和溢出图标颜色,以及添加的任何自定义ImageButtons。这是我的结果(请原谅英文的“颜色”拼写(!)..):

    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,即使已修复,这种方法也可能在未来失效...
    猜你喜欢
    • 2017-12-07
    • 1970-01-01
    • 1970-01-01
    • 2015-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多