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

新手上路,请多包涵

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

黑背箭头

我希望它是灰色的。我怎样才能做到这一点?

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

我这样做是为了设置一些样式。我已经使用 @drawable/abc_ic_ab_back_mtrl_am_alphahomeAsUpIndicator 。该drawable是透明的(只有alpha),但箭头显示为黑色。我想知道我是否可以像在 DrawerArrowStyle 中那样设置颜色。或者,如果唯一的解决方案是创建我的 @drawable/grey_arrow 并将其用于 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 ,它似乎是白色的,并使用照片编辑器将其绘制成我想要的颜色。它有效,尽管我更喜欢使用 @color/actionbar_text 就像在 DrawerArrowStyle 中一样。

原文由 Ferran Maylinch 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 339
2 个回答

您可以通过代码来实现它。获取后退箭头drawable,用滤镜修改颜色,设置为后退按钮。

 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

编辑:

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

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

原文由 Carles 发布,翻译遵循 CC BY-SA 3.0 许可协议

Looking at the Toolbar and TintManager source, drawable/abc_ic_ab_back_mtrl_am_alpha is tinted with the value of the style attribute colorControlNormal .

我确实尝试在我的项目中设置它(在我的主题中使用 <item name="colorControlNormal">@color/my_awesome_color</item> ),但它对我来说仍然是黑色的。

更新

找到了。您需要使用 --- 设置 actionBarTheme 属性( 不是 actionBarStyle colorControlNormal

例如:

 <style name="MyTheme" parent="Theme.AppCompat.Light">
    <item name="actionBarTheme">@style/MyApp.ActionBarTheme</item>
    <item name="actionBarStyle">@style/MyApp.ActionBar</item>
    <!-- color for widget theming, eg EditText. Doesn't effect ActionBar. -->
    <item name="colorControlNormal">@color/my_awesome_color</item>
    <!-- The animated arrow style -->
    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
</style>

<style name="MyApp.ActionBarTheme" parent="@style/ThemeOverlay.AppCompat.ActionBar">
    <!-- THIS is where you can color the arrow! -->
    <item name="colorControlNormal">@color/my_awesome_color</item>
</style>

<style name="MyApp.ActionBarStyle" parent="@style/Widget.AppCompat.Light.ActionBar">
    <item name="elevation">0dp</item>
    <!-- style for actionBar title -->
    <item name="titleTextStyle">@style/ActionBarTitleText</item>
    <!-- style for actionBar subtitle -->
    <item name="subtitleTextStyle">@style/ActionBarSubtitleText</item>

    <!--
    the actionBarTheme doesn't use the colorControlNormal attribute
    <item name="colorControlNormal">@color/my_awesome_color</item>
     -->
</style>

原文由 rockgecko 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题