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

use*_*832 8 android material-design android-toolbar material-components material-components-android

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

下面是我的款式和颜色。操作栏的背景跟随着colorPrimary,很棒。但是,两种颜色(浅色和深色)都是非常深的颜色,并且希望操作栏文本颜色始终为白色。由于我现在使用 DayNight.NoActionBar 作为父级,所以它是黑色的浅色和白色的深色。我有几个不同的操作栏实例,所以我不想改变每个单独的实例,而只是在样式中定义它。我该怎么做?

样式文件

<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>
Run Code Online (Sandbox Code Playgroud)

夜\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>
Run Code Online (Sandbox Code Playgroud)

值\颜色.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>
Run Code Online (Sandbox Code Playgroud)

Gab*_*tti 10

使用 Material 主题,您有不同的选项来自定义在 Toolbar

  • 使用android:theme覆盖默认值仅适用于您的Toolbar
<com.google.android.material.appbar.MaterialToolbar
    android:theme="@style/MyThemeOverlay_Toolbar"
    ...>
Run Code Online (Sandbox Code Playgroud)

并使用:

  <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>
Run Code Online (Sandbox Code Playgroud)
  • 在您的样式中设置样式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>
Run Code Online (Sandbox Code Playgroud)

然后使用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>
Run Code Online (Sandbox Code Playgroud)

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


use*_*832 5

首先,将这三个添加到您的主要样式中(您可以根据需要命名样式):

<item name="toolbarStyle">@style/ToolbarStyle</item>
<item name="actionOverflowButtonStyle">@style/ToolbarStyle.Overflow</item>
<item name="toolbarNavigationButtonStyle">@style/Toolbar.Button.Navigation.Tinted</item>
Run Code Online (Sandbox Code Playgroud)

然后定义这些样式:

<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>
Run Code Online (Sandbox Code Playgroud)