Android 如何使用 AppCompat 更改 ActionBar 的操作文本颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22651646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:17:59  来源:igfitidea点击:

How to change ActionBar's action text color using AppCompat

androidandroid-actionbarandroid-actionbar-compat

提问by Mani

I am trying to change the text color of a action menu item on the action bar. Using app compat. Also the overflow icon doesnt change too. Here is my custom styled styles.xml files.

我正在尝试更改操作栏上操作菜单项的文本颜色。使用应用程序兼容性。溢出图标也不会改变。这是我的自定义样式styles.xml 文件。

res/values/styles.xml

res/values/styles.xml

<style name="AppTheme" parent="@style/Theme.AppCompat.Light">
    <!-- Setting values in the default namespace affects API levels 7-13 -->
    <item name="actionBarStyle">@style/MyStyledActionBar</item>
</style>

<style name="MyStyledActionBar" parent="@style/Widget.AppCompat.Light.ActionBar">
    <!-- Setting values in the default namespace affects API levels 7-13 -->
    <item name="background">@drawable/bg_action_bar</item>
    <item name="titleTextStyle">@style/MyActionBarTitleText</item>
    <item name="actionMenuTextAppearance">@style/MyActionBarMenuText</item>
    <item name="actionMenuTextColor">@style/MyActionBarMenuText</item>
    <item name="actionOverflowButtonStyle">@style/MyActionButtonOverFlow</item>
</style>

<style name="MyActionBarTitleText" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textColor">@color/font_half_white</item>
</style>

<style name="MyActionBarMenuText" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Menu">
    <item name="android:textColor">@color/font_half_white</item>
</style>

<style name="MyActionButtonOverFlow" parent="@style/Widget.AppCompat.Light.ActionButton.Overflow">
    <item name="android:src">@drawable/ic_action_search</item>
</style>

res/values-14/styles.xml

res/values-14/styles.xml

<style name="AppTheme" parent="@style/Theme.AppCompat.Light">
    <!-- Setting values in the android namespace affects API levels 14+ -->
    <item name="android:actionBarStyle">@style/MyStyledActionBar</item>
</style>

<style name="MyStyledActionBar" parent="@style/Widget.AppCompat.Light.ActionBar">
    <!-- Setting values in the android namespace affects API levels 14+ -->
    <item name="android:background">@drawable/bg_action_bar</item>
    <item name="android:titleTextStyle">@style/MyActionBarTitleText</item>
    <item name="android:actionMenuTextAppearance">@style/MyActionBarMenuText</item>
    <item name="android:actionMenuTextColor">@style/MyActionBarMenuText</item>
    <item name="android:actionOverflowButtonStyle">@style/MyActionButtonOverFlow</item>
</style>

<style name="MyActionBarTitleText" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textColor">@color/font_half_white</item>
</style>

<style name="MyActionBarMenuText" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Menu">
    <item name="android:textColor">@color/font_half_white</item>
</style>

<style name="MyActionButtonOverFlow" parent="@style/Widget.AppCompat.Light.Base.ActionButton.Overflow">
    <item name="android:src">@drawable/ic_action_search</item>
</style>

res/colors.xml #F3F3F3

res/colors.xml #F3F3F3

The text color still black doesnt change to white. Has anybody successfully changed the action menu text color. Please suggest some insights. I am testing on 4.4 Nexus5 device.

文本颜色仍然是黑色不会变成白色。是否有人成功更改了操作菜单文本颜色。请提出一些见解。我正在 4.4 Nexus5 设备上进行测试。

enter image description here

在此处输入图片说明

回答by Raghunandan

Use

<item name="android:actionMenuTextColor">@color/font_half_white</item>
// added style directly 

Instead of

代替

<item name="android:actionMenuTextColor">@style/MyActionButtonStyle</item>

in res/values-14/styles.xml

res/values-14/styles.xml

In res/values/styles.xml

res/values/styles.xml

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

I used a Red Color for testing. See the Action menu Item Home that is red in the snap

我使用红色进行测试。查看快照中红色的操作菜单项主页

enter image description here

在此处输入图片说明

Edit:

编辑:

Full Code

完整代码

In res/values-14/styles.xml

res/values-14/styles.xml

<resources>

    <!--
        Base application theme for API 14+. This theme completely replaces
        AppBaseTheme from BOTH res/values/styles.xml and
        res/values-v11/styles.xml on API 14+ devices.
    -->
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- API 14 theme customizations can go here. -->
            <item name="android:actionMenuTextColor">@color/red</item>
    </style>

</resources>

Then in res/values/stles.xml

然后在 res/values/stles.xml

  <resources>
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light">
        <!-- API 11 theme customizations can go here. -->
         <item name="actionMenuTextColor">@color/red</item>
    </style>

</resources>

In manifest

在清单中

android:theme="@style/AppBaseTheme" >

android:theme="@style/AppBaseTheme" >

Another snap

另一个快照

enter image description here

在此处输入图片说明

回答by onexf

You can use this to change the text of the Action Bar irrespective of your appcompat version.Give the desired color's hex code and add this in the onCreate().

无论您的 appcompat 版本如何,您都可以使用它来更改操作栏的文本。提供所需颜色的十六进制代码并将其添加到 onCreate() 中。

actionBar.setTitle(Html.fromHtml("<font color='#ff0000'>Some Title</font>"));

回答by DoruChidean

  1. Check that in your layout xml file you don't have an android:themeattribute specified for your <Toolbar>, which would have priority over the main theme

  2. Override your actionBarStyleattribute as pointed out everywhere over the internets.

  1. 检查您的布局 xml 文件中是否没有android:theme为您的 指定属性<Toolbar>,该属性优先于主题

  2. 覆盖您actionBarStyle在互联网上随处可见的属性。