如何在新材质主题中更改后退箭头的颜色?

如何在新材质主题中更改后退箭头的颜色?

问题描述:

我已将我的 SDK 更新为 API 21,现在备份/备份图标是一个指向左侧的黑色箭头.

I've updated my SDK to API 21 and now the back/up icon is a black arrow pointing to the left.

我希望它是灰色的.我该怎么做?

I would like it to be grey. How can I do that?

例如,在 Play 商店中,箭头是白色的.

In the Play Store, for example, the arrow is white.

我这样做是为了设置一些样式.我已将 @drawable/abc_ic_ab_back_mtrl_am_alpha 用于 homeAsUpIndicator.该 drawable 是透明的(只有 alpha),但箭头显示为黑色.我想知道是否可以像在 DrawerArrowStyle 中那样设置颜色.或者,如果唯一的解决方案是创建我的 @drawable/grey_arrow 并将其用于 homeAsUpIndicator.

I've done this to set some styles. I have used @drawable/abc_ic_ab_back_mtrl_am_alpha for homeAsUpIndicator. That drawable is transparent (only alpha) but the arrow is displayed in black. I wonder if I can set the color like I do in the DrawerArrowStyle. Or if the only solution is to create my @drawable/grey_arrow and use it for homeAsUpIndicator.

<!-- Base application theme -->
<style name="AppTheme" parent="Theme.AppCompat.Light">

    <item name="android:actionBarStyle" tools:ignore="NewApi">@style/MyActionBar</item>
    <item name="actionBarStyle">@style/MyActionBar</item>

    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>

    <item name="homeAsUpIndicator">@drawable/abc_ic_ab_back_mtrl_am_alpha</item>
    <item name="android:homeAsUpIndicator" tools:ignore="NewApi">@drawable/abc_ic_ab_back_mtrl_am_alpha</item>
</style>

<!-- ActionBar style -->
<style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid">

    <item name="android:background">@color/actionbar_background</item>
    <!-- Support library compatibility -->
    <item name="background">@color/actionbar_background</item>
</style>

<!-- Style for the navigation drawer icon -->
<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
    <item name="spinBars">true</item>
    <item name="color">@color/actionbar_text</item>
</style>

到目前为止,我的解决方案是采用看起来是白色的 @drawable/abc_ic_ab_back_mtrl_am_alpha,并使用照片编辑器将其绘制成我想要的颜色.它有效,虽然我更喜欢在 DrawerArrowStyle 中使用 @color/actionbar_text.

My solution so far has been to take the @drawable/abc_ic_ab_back_mtrl_am_alpha, which seems to be white, and paint it in the color I desire using a photo editor. It works, although I would prefer to use @color/actionbar_text like in DrawerArrowStyle.

可以通过代码实现.获取可绘制的后退箭头,使用过滤器修改其颜色,并将其设置为后退按钮.

You can achieve it through code. Obtain the back arrow drawable, modify its color with a filter, and set it as back button.

final Drawable upArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
upArrow.setColorFilter(getResources().getColor(R.color.grey), PorterDuff.Mode.SRC_ATOP);
getSupportActionBar().setHomeAsUpIndicator(upArrow);

修订版 1:

从 API 23 (Marshmallow) 开始,可绘制资源 abc_ic_ab_back_mtrl_am_alpha 更改为 abc_ic_ab_back_material.

Starting from API 23 (Marshmallow) the drawable resource abc_ic_ab_back_mtrl_am_alpha is changed to abc_ic_ab_back_material.

您可以使用此代码来实现您想要的结果:

You can use this code to achieve the results you want:

toolbar.getNavigationIcon().setColorFilter(getResources().getColor(R.color.blue_gray_15), PorterDuff.Mode.SRC_ATOP);