如何在Android 4.3 API 18上更改ActionBar标题文本的颜色

4
我正在使用appcompat v7库来支持api级别为8及以上的操作栏。 我试图更改操作栏标题文本的颜色。
在GingerBread中,标题文本的颜色会改变,但是当我在JellyBean上运行应用程序时,颜色不会改变。
在GingerBread上,标题颜色变为红色: (插图)
在JellyBean上它仍然是黑色: (插图)
这是我的styles.xml文件:
<resources>

    <style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light">
        <item name="actionBarStyle">@style/ActionBarStyle</item>
    </style>

    <style name="ActionBarStyle" parent="@style/Widget.AppCompat.Light.ActionBar.Solid">
        <item name="titleTextStyle">@style/TitleBarTextColor</item>
    </style>

    <style name="TitleBarTextColor" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
        <item name="android:textColor">@color/red</item>
    </style>
    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

</resources>

v-11版本的styles.xml:

<resources>

    <style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light">
        <item name="actionBarStyle">@style/ActionBarStyle</item>
    </style>

</resources>

v-14 版本的 styles.xml:

<resources>

    <style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light">
        <item name="actionBarStyle">@style/ActionBarStyle</item>
    </style>
</resources>

清单:

android:theme="@style/AppTheme" 
2个回答

15

我解决了自己的问题:

我必须使用 android:actionBarStyle, android:titleTextStyleandroid:textColor 适用于 API 级别14及以上版本。

因此,适用于 v-11 的 styles.xml 将如下:

<resources>

    <style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light">
        <item name="android:actionBarStyle">@style/ActionBarStyle</item>
    </style>

    <style name="ActionBarStyle" parent="@style/Widget.AppCompat.Light.ActionBar.Solid">
        <item name="android:titleTextStyle">@style/TitleBarTextColor</item>
    </style>

    <style name="TitleBarTextColor" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
        <item name="android:textColor">@color/red</item>
    </style>
    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

</resources>

1
我不知道为什么,但只有在我删除 "android:" <item name="actionBarStyle">@style/MyActionBar</item> <item name="titleTextStyle">@style/TitleBarTextColor</item> 后才能正常工作。 - Anthone

0

我将在当前主题的样式中使用以下内容,以更新ActionBar中的文本颜色:

<item name="android:actionMenuTextColor">@color/Red</item>

这对我来说似乎在最新的操作系统版本中都有效。


我只想更改标题的颜色,这就是我使用 TitleBarTextColor 的原因。我认为你的解决方案会影响整个 ActionBar 上的文本,如子文本、菜单文本等。 - Rakesh
你说得对,那是真的。它修改了整个ActionBar。我不确定我是否在我所工作的任何应用程序中(或者还没有需要)实现了你所要求的内容。 - Jay Snayder

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接