无法更改应用程序中操作栏的颜色

Unable to change the color of action bar in the app

https://drive.google.com/file/d/1fniw1q9lx2U8D5CblZHAdBrOl2Oais0T/view?usp=sharing

我想在深色模式下更改操作栏的颜色,如您在我所附图片中看到的那样,“生日快乐”后面有一个黑色操作栏!

既然你说的是深色模式,那你也需要处理一下。

首先。创建一个名为 values-night .

的文件夹

然后,您需要复制 colors.xml(从文件夹 values)并粘贴到文件夹 values-night.

最后,将 colors.xml 处的颜色更改为 values-night 与深色模式相同。

对于Dark Mode,android查看下面目录themes.xml文件中定义的样式和主题

res/values-night/themes.xml

默认情况下,在 Light Mode 中,默认操作栏将使用 res/values/themes.xml 中定义的基础应用程序主题 colorPrimary。你的情况是 @color/white

默认情况下,在 Dark Mode 中,默认操作栏将始终为黑色,并且不会使用 res/values-night/themes.xml

中定义的 colorPrimary

解决方案: 我们需要强制操作栏使用夜间主题的 colorPrimary 属性或 colors.xml.

中的任何单独颜色

1.如果要依赖colorPrimay

  • 使用 actionBarStyle 属性
  • 将此样式 Widget.MaterialComponents.ActionBar.Primary 应用到夜间主题中的操作栏

res/values-night/themes.xml

<resources xmlns:tools="http://schemas.android.com/tools">

    <!-- Base application theme. -->
    <style name="Theme.HappyBirthday" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_200</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/black</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_200</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>

        <!-- Applying style like this  -->
        <item name="actionBarStyle">@style/Widget.MaterialComponents.ActionBar.Primary</item>
    </style>
    
</resources>

2。如果你想使用与 color.xml

不同的颜色
  • res/values-night/themes.xml 中创建新样式 MyActionBarDarkStyle,从 Widget.MaterialComponents.ActionBar.Primary
  • 延伸
  • 用任何颜色覆盖 background 属性
  • 使用 actionBarStyle 属性
  • 将新样式应用于夜间主题内的操作栏

res/values-night/themes.xml

<resources xmlns:tools="http://schemas.android.com/tools">

    <!-- Base application theme. -->
    <style name="Theme.HappyBirthday" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_200</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/black</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_200</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>

        <!-- Applying the new style that is defined below  -->
        <item name="actionBarStyle">@style/MyActionBarDarkStyle</item>
    </style>

    <!-- Our new style for ActionBar -->
    <style name="MyActionBarDarkStyle" parent="Widget.MaterialComponents.ActionBar.Primary">
         <item name="background">@color/warm_yellow</item>
    </style>

</resources>