Android浅色/深色主题操作栏文本 [英] Android light/dark theme action bar text

本文介绍了Android浅色/深色主题操作栏文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的游乐场Android应用程序中实现一个深色主题,并且正在努力使操作栏文本颜色为白色.

I'm implementing a dark theme in my playground Android app, and am struggling to get the action bar text color to be white.

下面是我的风格和颜色.操作栏的背景遵循colorPrimary,这很棒.但是,两种颜色(浅色和深色)都是非常暗的颜色,并且希望操作栏文本颜色始终为白色.由于我现在将DayNight.NoActionBar用作父级,因此它是黑色的,是浅色的,白色是深色的.我有几个不同的操作栏实例,所以我不想不必更改每个单独的实例,而只是在样式中对其进行定义.我该怎么做呢?

Below is my style and colors. The background of the action bar follows colorPrimary, which is great. However, both colors (light and dark) are pretty dark colors, and would like the action bar text color to always be white. Since I am using the DayNight.NoActionBar as the parent right now, it is black for light and white for dark. I have a few different instances of the action bar so I would prefer not to have to change each individual one, but rather just define it in the style. How do I go about doing this?

styles.xml

styles.xml

<style name="DarkThemeApp" parent="@style/Theme.MaterialComponents.DayNight.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="colorError">@color/colorError</item>
    <item name="android:textColor">?android:attr/textColorPrimary</item>
</style>

night \ colors.xml

night\colors.xml

<resources>
    <color name="colorPrimary">#3d85c6</color>
    <color name="colorPrimaryDark">#002e72</color>
    <color name="colorAccent">#e66f00</color>
    <color name="colorYellow">#FFE800</color>
    <color name="colorError">#E53935</color>
</resources>

值\颜色.xml

<resources>
    <color name="colorPrimary">#00348e</color>
    <color name="colorPrimaryDark">#002e72</color>
    <color name="colorAccent">#e66f00</color>
    <color name="colorYellow">#FFE800</color>
    <color name="colorError">#E53935</color>
</resources>

推荐答案

使用材质主题,您可以使用不同的选项来自定义工具栏

Using a material theme you have different options to customize the text color used in the Toolbar

  • 使用 android:theme 覆盖仅用于 Toolbar
  • 的默认值
  • Use the android:theme to override default values only for your Toolbar
<com.google.android.material.appbar.MaterialToolbar
    android:theme="@style/MyThemeOverlay_Toolbar"
    ...>

并使用:

  <style name="MyThemeOverlay_Toolbar" parent="ThemeOverlay.MaterialComponents.Toolbar.Primary">
    <!-- color used by the toolbar title -->
    <item name="android:textColorPrimary">@color/secondaryColor</item>
    <!-- color used by navigation icon and overflow icon -->
    <item name="colorOnPrimary">@color/secondaryColor</item>
  </style>

  • 在您的工具栏
  • 中设置样式

    <com.google.android.material.appbar.MaterialToolbar
        style="@style/MyToolbar"
        .../>
    

    然后使用 materialThemeOverlay 覆盖默认值(它需要材料组件库版本1.1.0):

    And then use the materialThemeOverlay to override the default values (it requires the material components library version 1.1.0):

      <!-- Toolbar -->
      <style name="MyToolbar" parent="Widget.MaterialComponents.Toolbar">
        <item name="materialThemeOverlay">@style/MyThemeOverlay_Toolbar</item>
      </style>
    
      <style name="MyThemeOverlay_Toolbar" parent="">
        <item name="android:textColorPrimary">@color/secondaryColor</item>
        <item name="colorOnPrimary">@color/secondaryColor</item>
      </style>
    

    这篇关于Android浅色/深色主题操作栏文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆