【问题标题】:Change ActionBar text color after inheriting Theme.AppCompat.DayNight继承 Theme.AppCompat.DayNight 后更改 ActionBar 文本颜色
【发布时间】:2019-11-01 18:08:36
【问题描述】:

我正在实现对暗模式的支持。但是在我将 Theme 父级更改为 Theme.AppCompat.DayNight 后,Action Bar 中的文本为黑色(白天模式下为黑色,深色模式下为白色)。我希望文本始终是白色的。我试图改变styles.xml中的文本颜色(值和值夜)

<style name="AppTheme" parent="Theme.AppCompat.DayNight">
  <item name="android:actionBarStyle">@style/MyActionBarStyle</item>
</style>

<style name="MyActionBarStyle" parent="Theme.AppCompat.DayNight.DarkActionBar">
  <item name="android:titleTextStyle">@style/MyActionBarTitleTextStyle</item>
</style>

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

但它不起作用。 ActionBar 中的标题文本颜色仍然是黑色。

【问题讨论】:

  • 为我工作

标签: android android-actionbar android-appcompat android-darkmode


【解决方案1】:

在你的主题中使用 ActionBar 你应该使用actionBarStyle 属性:

<style name="AppTheme" parent="Theme.AppCompat.DayNight">
   <item name="actionBarStyle">@style/....</item>
</style>

<style name="MyActionBarStyle" parent="Theme.AppCompat.DayNight.DarkActionBar">
  <item name="titleTextStyle">@style/...</item>
</style>

如果你使用Toolbar API,你也可以使用

 <Toolbar
   app:titleTextColor="@color/...."
   ../>

您可以使用材料组件库和Toolbar API:

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
   <item name="toolbarStyle">@style/my_Toolbar</item>
</style>

<style name=my_Toolbar" parent="@style/Widget.MaterialComponents.Toolbar">
    <item name="titleTextColor">@color/...</item>
</style>    

【讨论】:

    【解决方案2】:

    最好和最简单的解决方案是@MagogCZ 的回答 =>

    “为我工作&lt;style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar"&gt; – MagogCZ

    【讨论】:

      【解决方案3】:

      文本颜色需要使用actionBarTheme 而不是actionBarStyle

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-21
        相关资源
        最近更新 更多