Android ActionBar/Toolbar 浅色与深色主题的颜色不同

Android ActionBar/Toolbar colors different in Light vs Dark themes

我想了解为什么 ActionBar 在浅色和深色主题中的样式不同。下面是一个简单的设置屏幕,可以在具有相同主题的明暗主题之间切换。

values\themes.xml

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Dark application theme. -->
    <style name="Theme.SettingsApplication" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <item name="colorPrimary">@color/blue_normal</item>
        <item name="colorPrimaryVariant">@color/blue_dark</item>
        <item name="colorOnPrimary">@color/white</item>
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <item name="colorSurface">@color/red</item>
        <item name="colorOnSurface">@color/green</item>
    </style>
</resources>

values-night\themes.xml

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Dark application theme. -->
    <style name="Theme.SettingsApplication" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <item name="colorPrimary">@color/blue_normal</item>
        <item name="colorPrimaryVariant">@color/blue_dark</item>
        <item name="colorOnPrimary">@color/white</item>
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <item name="colorSurface">@color/red</item>
        <item name="colorOnSurface">@color/green</item>
    </style>
</resources>

values\colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="blue_normal">#FF3F7FBF</color>
    <color name="blue_dark">#FF2F6090</color>
    <color name="white">#FFFFFFFF</color>
    <color name="red">#FF0000</color>
    <color name="green">#00FF00</color>
</resources>

在 Light them 中,ActionBar 看起来是蓝色和白色,表明它正在使用 colorPrimarycolorOnPrimary。在深色主题中,它显示为红色和绿色,表示它正在使用 colorSurfacecolorOnSurface。为什么这些应用不同,我如何才能在 ActionBar 上获得一致的颜色应用?

Why are these applied differently

引自文档:

large surfaces should not use bright background colors because they can emit too much brightness. When applicable, the Material themes will provide this behavior out-of-the-box, e.g., by having the Light theme default to Widget.MaterialComponents.AppBarLayout.Primary and the Dark theme default to Widget.MaterialComponents.AppBarLayout.Surface.

因此,虽然浅色和深色主题的颜色相同,但 Primary 颜色是浅色模式的首选颜色,而 surface 颜色是深色模式的首选颜色.

how can I get consistent application of colors on the ActionBar?

这可以从之前的文档中得出结论..

所以,您只需要 values\themes.xml 包含:

  1. colorPrimary & colorSurface 具有相同的值
  2. colorOnPrimary & colorOnSurface 具有相同的值

将此应用于您的示例

values\themes.xml

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Dark application theme. -->
    <style name="Theme.SettingsApplication" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <item name="colorPrimary">@color/blue_normal</item>
        <item name="colorOnPrimary">@color/white</item>
        
        <item name="colorSurface">@color/blue_normal</item>
        <item name="colorOnSurface">@color/white</item>
    </style>
</resources>

更多信息,请check documentation