【问题标题】:Change ActionBar title text color using Light.DarkActionBar theme in AppCompat 21在 AppCompat 21 中使用 Light.DarkActionBar 主题更改 ActionBar 标题文本颜色
【发布时间】:2015-01-08 08:16:08
【问题描述】:

我正在使用 v7 appcompat 21 库在棒棒糖之前的设备上使用新的 Material 样式。我的styles.xml 看起来像这样:

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

我正在尝试更改操作栏上的文本颜色。但无论我为 textColorPrimary 或 textColorPrimaryInverse 放置什么,颜色始终是白色的。如果我从 Theme.AppCompat 继承,我可以覆盖“textColorPrimary”,如果我从 Theme.AppCompat.Light 继承,我可以覆盖“textColorPrimaryInverse”。但是在使用 Light.DarkActionBar 主题时两者都不起作用。

我肯定会使用 AppTheme,因为设置诸如 colorPrimary 之类的属性来更改操作栏背景颜色可以正常工作。我没有使用任何其他资源限定符样式文件。

我已经翻阅了 android 样式文件,但似乎无法确定要覆盖的属性。有任何想法吗?这是一个 appcompat 错误吗?

【问题讨论】:

    标签: android android-actionbar android-appcompat android-actionbar-compat material-design


    【解决方案1】:

    您可以使用主题的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>
    

    【讨论】:

    • 由于某种原因,您在 Android 5 或 Android 4 设备上运行时设置 textColor 的方式没有任何效果(我已经测试过这两个设备) - 有什么想法吗?我正在使用带有 AppCompatActivity 的工具栏,并在我的清单中应用上述主题。
    • @AgentKnopf 这仅适用于 ActionBar。您必须将 ToolBar 设置为 ActionBar 才能使其正常工作。
    • 这对我不起作用。我正在使用工具栏并将其设置为操作栏。我为我的应用程序使用了 Theme.AppCompat.Light.NoActionBar,为 AppBarLayout 使用了 ThemeOverlay.AppCompat.Dark.ActionBar,为工具栏使用了样式 ThemeOverlay.AppCompat.Light
    • 我真的很想知道如果可以在一个父 xml 标签下压缩三种样式资源会是什么样子
    • 字幕的样式怎么样?那会怎样?
    【解决方案2】:

    这将使您的工具栏标题使用白色:

    <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" />
    

    要了解ThemeOverlay.AppCompat.Dark.ActionBarTheme.AppCompat.Light.DarkActionBar 之间的区别,请查看this answer

    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 应用浅色,因为此主题中使用的标题颜色 是白色的。

    【讨论】:

    • 这个答案对于使它工作所需的内容不是很具体。是哪个部分使它变白?如果有人想让它变红怎么办? app: 前缀定义在哪里?
    • @Thibault D。我在回答中添加了更多细节。
    • 感谢您的快速回答,我今天就试一试!
    【解决方案3】:

    确保在所有值文件夹中进行更改,例如“values-v11”、“values-v14”等。如果存在的话。

    否则,请确保在应用中使用您上面显示的相同“AppTheme”。

    请向您发布完整的 style.xml 和引用该样式的清单代码,这将对您的问题提供更多见解。

    您也可以尝试在工具栏上设置 app:themeapp:popupTheme 属性。

    【讨论】:

    • &lt;android.support.v7.widget.Toolbar android:id="@+id/my_toolbar" android:layout_height="wrap_content" android:layout_width="match_parent" android:minHeight="?attr/actionBarSize" android:background="?attr/colorPrimary" app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:popupTheme="@style/ThemeOverlay.AppCompat.Light"&gt; 工作!
    【解决方案4】:

    尝试在 Activity 的 onCreate 中添加它。适用于几乎所有 Android 版本。

    对于 Java 活动:

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

    对于片段:

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

    【讨论】:

    • 别这样,用样式
    【解决方案5】:

    不幸的是,我费了很大劲才改变了应用名称的颜色。我有很多主题。最后,对我有用的是更改以下两个属性。

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

    【讨论】:

      【解决方案6】:

      我使用了一个生成的布局 xml 文件,并没有注意到 android:theme 属性被覆盖。

      你检查过你的吗? :)

      【讨论】:

        【解决方案7】:

        试试这个:

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

        【讨论】:

        • 这只会给溢出菜单中的文本着色。
        猜你喜欢
        • 2017-07-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-04
        • 2014-12-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多