在不同的Android版本上动态更改工具栏菜单项的文本颜色 [英] Change toolbar menu item text color dynamically on different Android version

查看:86
本文介绍了在不同的Android版本上动态更改工具栏菜单项的文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要在不同的片段中更改工具栏菜单项的文本颜色.现在,当必须运行时,我有两个片段.我将静态样式设置为AppTheme.

Need to change toolbar menu item text color in different fragments. Right now i have two fragments when this must work. I set static style AppTheme with.

<item name="actionMenuTextColor">@color/black</item>

第一个片段中的文本必须为黑色,第二个白色为文本.

In first fragment text must be black and in second white.

当在菜单项的setTitle中设置SpannableString而不是String时,我使用了此选项.8.0和更高版本都可以正常工作.检查它在5.0、6.0、7.0上的工作方式时,没有任何变化.

I used this option when set SpannableString instead of String in setTitle on menuItem. This work good with version 8.0 and greater. When check how it works on 5.0, 6.0, 7.0 nothing change.

在AndroidManifest的应用程序"部分中设置的样式

Style set in Application section in AndroidManifest

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
 <item name="colorPrimary">@color/colorPrimary</item>
 <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
 <item name="colorAccent">@color/colorAccent</item>
 <item name="actionMenuTextColor">@color/black</item>
</style>

在片段中设置> onCreateOptionsMenu方法

Set in fragment > onCreateOptionsMenu method

MenuItem menuItem= menu.findItem(R.id.action_done);
SpannableString s = new SpannableString(menuItem.getTitle().toString());
s.setSpan(new ForegroundColorSpan(Color.WHITE), 0, s.length(), 0);
menuItem.setTitle(s);

希望有人知道如何动态更改此参数.

Hope someone has idea how dynamically chnage this parameter.

推荐答案

这些菜单项标题的默认样式将 textAllCaps 属性设置为 true ,这会导致在标题的 TextView 上设置的 AllCapsTransformationMethod .这种转换方法比奥利奥(Oreo)之前有一个错误,即它会将文本视为纯 String ,这实际上会剥离您可能设置的所有格式设置范围.

The default style for those menu items' titles has the textAllCaps attribute set to true, which causes an AllCapsTransformationMethod to be set on the titles' TextViews. That transformation method had a bug prior to Oreo, in that it would treat the text as a flat String, which essentially strips any formatting spans that you may have set.

此问题在Oreo中已得到纠正,但是由于 AllCapsTransformationMethod 是平台类,因此即使具有support/androidx库,该修复也不具有追溯力.对于我们自己的修复,我们可以简单地为这些项目标题关闭 textAllCaps ,并在创建格式化文本之前在代码中自行处理大写字母.

This was corrected in Oreo, but since AllCapsTransformationMethod is a platform class, that fix isn't retroactive, even with the support/androidx libraries. For our own fix, we can simply turn off textAllCaps for those item titles, and handle the capitalization ourselves in code, before creating the formatted text.

对于support/androidx ActionBar / Toolbar ,我们将主题和样式修改为:

For the support/androidx ActionBar/Toolbar, we modify the theme and style as such:

<style name="AppTheme" parent="Theme.AppCompat...">
    ...
    <item name="actionMenuTextAppearance">@style/TextAppearance.ActionBar.Menu.NoCaps</item>
</style>

<style name="TextAppearance.ActionBar.Menu.NoCaps"
       parent="TextAppearance.AppCompat.Widget.ActionBar.Menu">

    <item name="textAllCaps">false</item>
</style>

然后在代码中将标题转换为大写:

Then convert the title to uppercase in the code:

SpannableString s = new SpannableString(menuItem.getTitle().toString().toUpperCase());

对于平台 ActionBar / Toolbar ,我们需要在主题和样式中设置相应的平台属性.例如:

For the platform ActionBar/Toolbar, we need to set the corresponding platform attributes in the theme and style. For example:

<style name="AppTheme" parent="android:Theme.Material...">
    ...
    <item name="android:actionMenuTextAppearance">@style/TextAppearance.ActionBar.Menu.NoCaps</item>
</style>

<style name="TextAppearance.ActionBar.Menu.NoCaps"
       parent="android:TextAppearance.Material.Widget.ActionBar.Menu">

    <item name="android:textAllCaps">false</item>
</style>

代码更改相同.

由于这是奥利奥(Oreo)中已解决的问题,我们可以通过在相关的 res/values [-v ??]/文件夹中进行这些更改,并使用默认值,将上述内容仅应用于受影响的版本 res/values-v26/中的设置,以及更高的设置.

As this was fixed in Oreo, we could apply the above only on the affected versions by making these changes in the relevant res/values[-v??]/ folders, and using the default settings in res/values-v26/, and any higher.

但是,为所有版本设置这种样式要简单得多,并且由于我们正在执行的文本处理比 AllCapsTransformationMethod 所进行的处理强度稍低,所以没有理由不这样做到处使用它.

However, it is much simpler to just set this style for all versions, and since the text processing we're doing is a little less intensive than what AllCapsTransformationMethod would do, there's really no reason not to use this everywhere.

这篇关于在不同的Android版本上动态更改工具栏菜单项的文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆