0

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

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

4

2 回答 2

3

对于Dark Mode,android看themes.xml下面目录下的文件中定义的styles和themes

res/values-night/themes.xml

默认情况下,在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>

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>
于 2021-07-04T12:29:30.900 回答
0

既然你说它处于黑暗模式,所以你也需要处理它。

第一的。创建一个名为values-night.

在此处输入图像描述

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

最后,将颜色更改colors.xmlvalues-night暗模式。

于 2021-07-04T08:23:23.857 回答