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

Sar*_*pta 3 android android-layout android-fragments android-studio

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

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

Sye*_*mil 12

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

res/values-night/themes.xml
Run Code Online (Sandbox Code Playgroud)

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

默认情况下,在Dark Mode默认的action bar中会一直是黑色的并且不会使用colorPrimary里面定义的res/values-night/themes.xml

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

1.如果你想依赖colorPrimay

  • 使用属性将此样式应用Widget.MaterialComponents.ActionBar.Primary到夜间主题内的操作栏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 style like this  -->
        <item name="actionBarStyle">@style/Widget.MaterialComponents.ActionBar.Primary</item>
    </style>
    
</resources>
Run Code Online (Sandbox Code Playgroud)

2. 如果你想使用不同的颜色color.xml

  • MyActionBarDarkStyle在内部创建一个新的样式res/values-night/themes.xml,从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>
Run Code Online (Sandbox Code Playgroud)