问题描述
如何着色菜单图标已经覆盖了几次,例如:
How to tint menu icons is already covered a few times, like here:Toolbar icon tinting on Android
除此解决方案外,还存在导航图标问题。
将主题(叠加)应用于工具栏只会为文本和列入白名单的图标着色(请参阅:)
Additionally to this solution there is still the problem of the navigation icon.Applying a Theme(Overlay) to your Toolbar just tints the text and the whitelisted icons (see: https://stackoverflow.com/a/26817918/2417724)
如果您设置了一个自定义图标(这种情况恰好相当容易,因为您需要更改它你不想显示默认的后退箭头)然后这个自定义图标没有着色。
If you set a custom icon (which happens to be quite easy the case, as you need to change it if you don't want to display the default back arrow) then this custom icon does not get tinted.
那你如何处理你的图标呢?
我的所有图标都是默认黑色,我不想在工具栏中使用特殊的白色版本。
How do you handle your Icons then?All my icons are per default black and I don't want to have special white versions just to use them in the Toolbar then.
推荐答案
appcompat导航按钮 - 只是一个 AppCompatImageButton
- 可以通过 toolbarNavigationButtonStyle
属性设置样式。 AppCompat
主题中的默认样式是 Widget.AppCompat.Toolbar.Button.Navigation
,我们可以扩展该样式添加色调
属性值。例如:
The appcompat navigation button - which is simply an AppCompatImageButton
- can be styled through the toolbarNavigationButtonStyle
attribute. The default style for that in the AppCompat
themes is Widget.AppCompat.Toolbar.Button.Navigation
, and we can extend that style to add a tint
attribute value. For example:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
<item name="toolbarNavigationButtonStyle">@style/Toolbar.Button.Navigation.Tinted</item>
</style>
<style name="Toolbar.Button.Navigation.Tinted" parent="Widget.AppCompat.Toolbar.Button.Navigation">
<item name="tint">@color/nav_button_tint</item>
</style>
有几点需要注意使用此方法时。
There are a couple of caveats to be aware of when using this method.
在支持库版本25.4.0之前, AppCompatImageButton
没有提供自己的 tint
属性,因此应用程序命名空间中的 tint
属性将不适用(除非在别处定义,否则将不存在)。如果使用支持库版本25.3.0或更早版本,则必须使用平台 android:tint
属性。
Prior to support library version 25.4.0, AppCompatImageButton
did not offer its own tint
attribute, and therefore a tint
attribute in the app's namespace will not apply (and just won't exist, unless defined elsewhere). It is necessary to use the platform android:tint
attribute if using support library version 25.3.0 or earlier.
不幸的是,这导致了另一个问题,因为Lollipop之前的平台色调(API级别21)只能处理简单的单色值,并使用 ColorStateList
(< selector>
)资源值将导致抛出异常
。如果 android:tint
值是一个简单的颜色,这不会造成任何问题,但通常需要着色导航图标以匹配另一个主题颜色属性,这可能很好a ColorStateList
。在这种情况下,有必要在 res / values /
和 res / values-21 /
中创建单独的样式,在 res / values /
中指定 android:tint
的简单颜色值。
Unfortunately, this leads to another catch, in that the platform tint prior to Lollipop (API level 21) can handle only simple, single color values, and using a ColorStateList
(<selector>
) resource value will cause an Exception
to be thrown. This poses no problems if the android:tint
value is a simple color, but it is often desired to tint the navigation icon to match another theme color attribute, which may very well be a ColorStateList
. In this case, it would be necessary to create separate styles in res/values/
and res/values-21/
, specifying a simple color value for android:tint
in res/values/
.
例如,如果着色以匹配主题的主要文本颜色:
For example, if tinting to match the theme's primary text color:
res / values / styles.xml
<item name="android:tint">@color/normal_text_color</item>
res / values-v21 / styles.xml
<item name="android:tint">?android:textColorPrimary</item>
如果您使用的支持库版本低于此值,您只需要关注上述注释25.4.0。
You need only concern yourself with the notes above if you're stuck using a support library version less than 25.4.0.
这篇关于工具栏中的色调导航图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!