【问题标题】:set text color of Appcompat action bar does not work设置 Appcompat 操作栏的文本颜色不起作用
【发布时间】:2015-03-18 04:16:49
【问题描述】:

基本上我遵循了关于自定义操作栏https://developer.android.com/training/basics/actionbar/styling.html的指南

并在我的styles.xml 中有这种风格

    <style name="Blue" parent="Theme.AppCompat">
    <!-- colorPrimary is used for the default action bar background -->
    <item name="colorPrimary">@color/light_blue</item>
    <!-- colorPrimaryDark is used for the status bar -->
    <item name="colorPrimaryDark">@color/dark_blue</item>
    <!-- colorAccent is used as the default value for colorControlActivated
         which is used to tint widgets -->
    <item name="colorAccent">@color/light_blue</item>

    <item name="android:actionBarStyle">@style/MyActionBar</item>
    <item name="android:actionBarTabTextStyle">@style/MyActionBarTabText</item>
    <item name="android:actionMenuTextColor">@color/white</item>

    <!-- Support library compatibility -->
    <item name="actionBarStyle">@style/MyActionBar</item>
    <item name="actionBarTabTextStyle">@style/MyActionBarTabText</item>
    <item name="actionMenuTextColor">@color/white</item>
</style>

使用此自定义操作栏将标题文本更改为白色

    <!-- ActionBar styles -->
    <style name="MyActionBar"
    parent="@style/Widget.AppCompat.ActionBar">
    <item name="android:titleTextStyle">@style/MyActionBarTitleText</item>
    <!-- Support library compatibility -->
    <item name="titleTextStyle">@style/MyActionBarTitleText</item>
    <item name="android:subtitleTextStyle">@style/MyActionBarSubtitleText</item>
</style>

<!-- ActionBar title text -->
<style name="MyActionBarTitleText"
    parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textColor">@color/white</item>
    <!-- The textColor property is backward compatible with the Support Library -->
    <item name="textColor">@color/white</item>
</style>

<!-- ActionBar subtitle text -->
<style name="MyActionBarSubtitleText"
    parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textColor">@color/white</item>
    <item name="android:textSize">10sp</item>
    <!-- The textColor property is backward compatible with the Support Library -->
    <item name="textColor">@color/white</item>
    <item name="textSize">10sp</item>
</style>

<!-- ActionBar tabs text -->
<style name="MyActionBarTabText"
    parent="@style/Widget.AppCompat.ActionBar.TabText">
    <item name="android:textColor">@color/white</item>
    <item name="textColor">@color/white</item>
    <!-- The textColor property is backward compatible with the Support Library -->
</style>

但显然文本和图标颜色仍然是黑色。我错过了什么吗?

【问题讨论】:

  • 如果删除除colorPrimary/colorPrimaryDark/colorAccent 之外的所有样式会怎样?使用 Theme.AppCompat 时,默认应为深色背景上的白色文本。
  • 另请注意,AppCompat v21 不再使用 android: 前缀属性,因此在任何情况下都可以完全删除这些属性。
  • 你是对的,我删除了android: 前缀,它仍然有效。现在的问题是,文本颜色发生了变化,但它以某种方式覆盖了操作栏的colorPrimary,因此使用Theme.AppCompat.Light,我得到一个灰色的操作栏背景,标题文本颜色发生了变化。
  • @ianhanniballake 我刚刚删除了所有文本样式并将父主题设置为Theme.AppCompat.Light.DarkActionBar,现在它可以完美运行(我只想将文本更改为白色)。谢谢:D

标签: android android-actionbar styles


【解决方案1】:

如果您只希望文本为白色,那是 Theme.AppCompat 的默认设置,或者,如果您想要轻松的活动,请使用 Theme.AppCompat.Light.DarkActionBar

【讨论】:

  • 自定义颜色怎么样? actionMenuTextColor 不再工作了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-11
  • 1970-01-01
  • 1970-01-01
  • 2014-11-22
  • 1970-01-01
  • 2015-08-22
  • 2014-06-03
相关资源
最近更新 更多