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

时间:2016-03-15 19:59:29

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

如何以编程方式在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

3 个答案:

答案 0 :(得分: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,请更改所有工具栏图标颜色。

希望这会有所帮助!!

答案 1 :(得分:3)

我已接受most helpful answerand commented on it)说明我使用其链接代码段的组合形成单个AppBarLayout / Toolbar着色方法。它包括背景,标题,副标题,背面/抽屉图标和溢出图标颜色,以及添加的任何自定义ImageButtons。这是我的结果(原谅英文'color'拼写(!).. ):

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

答案 2 :(得分:0)

您可以通过以下代码将cursor select更改为home

up icon