https://drive.google.com/file/d/1fniw1q9lx2U8D5CblZHAdBrOl2Oais0T/view?usp=sharing
我想在黑暗模式下更改我的操作栏的颜色,正如您在我所附的图片中看到的那样,“生日快乐”后面有一个黑色的操作栏!
https://drive.google.com/file/d/1fniw1q9lx2U8D5CblZHAdBrOl2Oais0T/view?usp=sharing
我想在黑暗模式下更改我的操作栏的颜色,正如您在我所附的图片中看到的那样,“生日快乐”后面有一个黑色的操作栏!
对于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应用于夜间主题内的操作栏actionBarStyleres/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.Primarybackground用任何颜色覆盖属性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>