代码之家  ›  专栏  ›  技术社区  ›  Seb Jachec

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

  •  5
  • Seb Jachec  · 技术社区  · 9 年前

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

    3 回复  |  直到 9 年前
        1
  •  10
  •   Community CDub    8 年前

    通过支持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 是一个很好的帖子,可以更改所有工具栏图标的颜色。

    希望这有帮助!!

        2
  •  6
  •   Community CDub    8 年前

    我已接受 most helpful answer ( and commented on it )解释说我使用了其链接代码段的组合来形成一个 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);
        }
    }
    
        3
  •  1
  •   IsaiahJ    8 年前

    你可以改变 home up icon 通过以下代码:

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