简体   繁体   English

使用 AppCompat 21 中的 Light.DarkActionBar 主题更改 ActionBar 标题文本颜色

[英]Change ActionBar title text color using Light.DarkActionBar theme in AppCompat 21

I'm using the v7 appcompat 21 library to use the new Material styles on pre-Lollipop devices.我正在使用 v7 appcompat 21 库在 Lollipop 之前的设备上使用新的 Material 样式。 My styles.xml looks like this:我的styles.xml 看起来像这样:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:textColorPrimary">#ff0000</item>
    <item name="android:textColorPrimaryInverse">#ff0000</item>
</style>

I am trying to change the text color on the action bar.我正在尝试更改操作栏上的文本颜色。 But no matter what I put for textColorPrimary or textColorPrimaryInverse, the color is always white.但无论我为 textColorPrimary 或 textColorPrimaryInverse 设置什么,颜色始终为白色。 If I inherit from Theme.AppCompat, I can override "textColorPrimary", and if I inherit from Theme.AppCompat.Light, I can override "textColorPrimaryInverse".如果我从 Theme.AppCompat 继承,我可以覆盖“textColorPrimary”,如果我从 Theme.AppCompat.Light 继承,我可以覆盖“textColorPrimaryInverse”。 But neither work when using the Light.DarkActionBar theme.但是在使用 Light.DarkActionBar 主题时两者都不起作用。

I am definitely using the AppTheme because setting attributes like colorPrimary to change the actionbar background color works fine.我肯定在使用 AppTheme,因为设置 colorPrimary 等属性来更改操作栏背景颜色效果很好。 I am not using any other resource qualifier style files.我没有使用任何其他资源限定符样式文件。

I've dug through the android styles files and can't seem to figure out what attribute to override.我翻遍了 android 样式文件,似乎无法弄清楚要覆盖的属性。 Any ideas?有任何想法吗? Is this an appcompat bug?这是一个appcompat错误吗?

You can change it with actionBarStyle attribute of theme.您可以使用主题的actionBarStyle属性更改它。

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

<style name="MyActionBar" parent="@style/Widget.AppCompat.ActionBar.Solid">
    <item name="titleTextStyle">@style/MyTitleTextStyle</item>
</style>

<style name="MyTitleTextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textColor">CHANGE_COLOR_HERE</item>
</style>

This will make your Toolbar Title use white color:这将使您的工具栏标题使用白色:

<android.support.v7.widget.Toolbar
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary" />

To understand what is the difference between ThemeOverlay.AppCompat.Dark.ActionBar and Theme.AppCompat.Light.DarkActionBar check this answer要了解ThemeOverlay.AppCompat.Dark.ActionBarTheme.AppCompat.Light.DarkActionBar之间的ThemeOverlay.AppCompat.Dark.ActionBarTheme.AppCompat.Light.DarkActionBar查看此答案

It's app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" that applies a light colour because the title colour which is used in this theme is white. app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"应用了浅色,因为在这个主题中使用的标题颜色是白色。

make sure you make the changes in all your values folder, like 'values-v11', 'values-v14' etc.. if it exists.确保在所有值文件夹中进行更改,例如“values-v11”、“values-v14”等。如果存在。

Else make sure the same 'AppTheme' you shown above is being used in the app.否则,请确保在应用程序中使用了您在上面显示的相同“AppTheme”。

Please post you full style.xml and the manifest code referring the style which will give more insights on your problem.请发布您完整的 style.xml 和引用该样式的清单代码,这将使您更深入地了解您的问题。

Also you can try setting app:theme and app:popupTheme attributes on you ToolBar.您也可以尝试在 ToolBar 上设置app:themeapp:popupTheme属性。

Try adding this in the onCreate of your Activity.尝试将其添加到您的活动的 onCreate 中。 Works on almost every Android version.适用于几乎所有 Android 版本。

For Java Activities:对于 Java 活动:

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

For Fragments:对于片段:

getSupportActionBar().setTitle(Html.fromHtml("<font color='#ffff00'>Your Title</font>"));

I used a generated layout xml file and didn't notice that the android:theme attribute was overriden.我使用了一个生成的布局 xml 文件,并没有注意到android:theme属性被覆盖了。

Did you check yours?你检查过你的吗? :) :)

Unfortunately I struggled a lot to change the color of App name.不幸的是,我努力改变应用程序名称的颜色。 I was having many themes.我有很多主题。 Finally what worked for me is changing below two attributes.最后,对我有用的是改变以下两个属性。

    <item name="android:textColorPrimary">#FF0000</item>
    <item name="android:textColorSecondary">#FF0000</item>

尝试这个:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Theme.AppCompat.Light.DarkActionBar提供黑色操作栏标题文本 - Theme.AppCompat.Light.DarkActionBar giving black actionbar title text 材质主题Light.DarkActionBar中的文本颜色不是ligth - Text color isn't ligth in Material Theme Light.DarkActionBar 带有Theme.AppCompat.Light.DarkActionBar的ActionBar微调框深色文本 - ActionBar Spinner dark text with Theme.AppCompat.Light.DarkActionBar 使用Theme.AppCompat.Light.DarkActionBar时不显示ActionBar - ActionBar does not appear when using Theme.AppCompat.Light.DarkActionBar 更改操作栏标题颜色Theme.AppCompat.Light.DarkActionBar android - change action bar title color Theme.AppCompat.Light.DarkActionBar android 如何在Theme.AppCompat.Light.DarkActionBar主题中更改菜单项的文本颜色? - How to change the text color of a menu item in Theme.AppCompat.Light.DarkActionBar theme? 如何更改textview的默认颜色(Theme.AppCompat.Light.DarkActionBar) - How to change textview default color (Theme.AppCompat.Light.DarkActionBar) 在Android上更改AppCompat Light DarkActionBar主题的背景和文本颜色 - Changing Background and text color of AppCompat Light DarkActionBar Theme on android Theme.AppCompat.Light.DarkActionBar的默认文本颜色是什么? - What is the default text color for Theme.AppCompat.Light.DarkActionBar? 使用Theme.Material.Light.DarkActionBar与Theme.AppCompat.Light.DarkActionBar崩溃Android L上的应用程序(api v21) - Using Theme.Material.Light.DarkActionBar with Theme.AppCompat.Light.DarkActionBar crashes the app on android L(api v21)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM