简体   繁体   English

Android 浅色/深色主题操作栏文字

[英]Android light/dark theme action bar text

I'm implementing a dark theme in my playground Android app, and am struggling to get the action bar text color to be white.我正在我的 Playground Android 应用程序中实现一个深色主题,并且正在努力使操作栏文本颜色变为白色。

Below is my style and colors.下面是我的款式和颜色。 The background of the action bar follows colorPrimary, which is great.操作栏的背景跟随着colorPrimary,很棒。 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.由于我现在使用 DayNight.NoActionBar 作为父级,所以它是黑色的浅色和白色的深色。 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样式文件

<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夜\\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>

values\\colors.xml值\\颜色.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使用材质主题,您可以使用不同的选项来自定义Toolbar使用的文本颜色

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

and use:并使用:

  <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>
  • Set a style in your ToolbarToolbar设置样式
<com.google.android.material.appbar.MaterialToolbar
    style="@style/MyToolbar"
    .../>

And then use the materialThemeOverlay to override the default values (it requires the material components library version 1.1.0):然后使用materialThemeOverlay覆盖默认值(它需要材质组件库版本 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>

在此处输入图片说明 在此处输入图片说明

First, add these three to your main style (you can name the styles whatever you want):首先,将这三个添加到您的主要样式中(您可以随意命名样式):

<item name="toolbarStyle">@style/ToolbarStyle</item>
<item name="actionOverflowButtonStyle">@style/ToolbarStyle.Overflow</item>
<item name="toolbarNavigationButtonStyle">@style/Toolbar.Button.Navigation.Tinted</item>

Then define those styles:然后定义这些样式:

<style name="ToolbarStyle" parent="@style/Widget.AppCompat.Toolbar">
    <!-- Makes the toolbar text whatever color you want for both light/dark-->
    <item name="titleTextColor">@color/actionBarText</item>
    <item name="android:background">?attr/colorPrimary</item>
</style>

<style name="ToolbarStyle.Overflow" parent="Widget.AppCompat.ActionButton.Overflow">
    <!-- For toolbar menu -->
    <item name="android:tint">@color/actionBarText</item>
</style>

<style name="Toolbar.Button.Navigation.Tinted" parent="Widget.AppCompat.Toolbar.Button.Navigation">
    <!-- For the hamburger menu, back button, etc -->
    <item name="tint">@color/actionBarText</item>
</style>

in your styles.xml use this code在你的styles.xml 中使用此代码

<style name="DarkThemeApp" parent="Theme.AppCompat.DayNight.DarkActionBar">
        <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>

I think the answer to this question might vary depending on what other components you use in your app.我认为这个问题的答案可能会因您在应用程序中使用的其他组件而异。 In my case the following was a working solution:在我的情况下,以下是一个有效的解决方案:

Define these three styles in styles.xmlstyles.xml定义这三种样式

  <style name="ToolbarStyle" parent="@style/Widget.MaterialComponents.Toolbar">
    <item name="titleTextColor">@android:color/white</item>
    <item name="android:background">@color/primary</item>
  </style>

  <style name="ToolbarStyle.Overflow" parent="@style/Widget.AppCompat.ActionButton.Overflow">
    <item name="android:tint">@android:color/white</item>
  </style>

  <style name="ToolbarStyle.DrawerIcon" parent="Widget.AppCompat.DrawerArrowToggle">
    <item name="color">@android:color/white</item>
  </style>

and then set them in your theme(s):然后将它们设置在您的主题中:

<style name="DarkThemeApp" parent="@style/Theme.MaterialComponents.DayNight.NoActionBar">
  <!-- color for stuff in action bar -->
  <item name="toolbarStyle">@style/ToolbarStyle</item>
  <item name="actionOverflowButtonStyle">@style/ToolbarStyle.Overflow</item>
  <item name="drawerArrowStyle">@style/ToolbarStyle.DrawerIcon</item>

  <!-- rest of your attributes go here ... -->
</style>

In new version of Android Studio, we have two files for theme colors.在新版本的 Android Studio 中,我们有两个主题颜色文件。 To change the dark theme, you should use this file:要更改深色主题,您应该使用此文件:

Res > values > themes > themes.xml (night)

And for day version:对于日版:

Res > values > themes > themes.xml

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM