简体   繁体   English

Jetpack Compose 状态栏颜色未在深色主题中更新

[英]Jetpack Compose status bar color not updated in Dark Theme

I want to set different colors for dark theme and light theme.我想为深色主题和浅色主题设置不同的颜色。 Everything works fine except status bar color.除了状态栏颜色外,一切正常。

In dark theme -> my status bar color not updated.在深色主题中 -> 我的状态栏颜色未更新。

My theme :我的主题:

private val DarkColorPalette = darkColors(
    primary = Color.Black,
    primaryVariant = Color.Black,
    secondary = Color.LightGray
)

private val LightColorPalette = lightColors(
    primary = Purple500,
    primaryVariant = Purple700,
    secondary = Teal200
)

My output in Dark theme:我在黑暗主题中的输出:

在此处输入图片说明

Edit:编辑:

I already saw this answer - how to change statusbar color in jetpack compose?我已经看到了这个答案 - 如何在 jetpack compose 中更改状态栏颜色?

They recommend to use the library.他们建议使用图书馆。 I don't want to use any 3rd party library.我不想使用任何 3rd 方库。

Edit 2:编辑2:

my manifest file我的清单文件

   <activity
        android:name=".ActivityTextStyles"
        android:exported="true"
        android:label="@string/app_name"
        android:theme="@style/Theme.ComposeText.NoActionBar" />

I was able to set status bar color with this one line code in onCreate()我能够在 onCreate() 中使用这一行代码设置状态栏颜色

  this.window.statusBarColor = ContextCompat.getColor(this,R.color.black)

My code:我的代码:

    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContent {
      
            this.window.statusBarColor = ContextCompat.getColor(this,R.color.black)
            //your composables
         }
     }

Here's what I discovered when I faced this same issue and decided to look deeper for the cause.当我遇到同样的问题并决定更深入地寻找原因时,这是我发现的。

Turns out, out of the box, my Compose project reads the value for status bar color from the themes.xml file in res/values .事实证明,我的 Compose 项目开箱即用地res/values 中themes.xml文件读取状态栏颜色的 The value is set using colorPrimaryVariant updating that value to a new color updated the color of the status bar.该值是使用colorPrimaryVariant设置的,将该值更新为更新状态栏颜色的新颜色。

Overall, I will recommend updating the values of themes.xml and colors.xml to the custom colors you wish to use for your project.总的来说,我建议将 themes.xml 和 colors.xml 的值更新为您希望用于项目的自定义颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Jetpack Compose:深色主题中的 AndroidView 颜色 - Jetpack Compose: AndroidView Color in Dark Theme 在 jetpack compose 中更改状态栏颜色时遇到问题 - facing problem in changing status bar color in jetpack compose Jetpack Compose 的状态栏中未显示模态底板稀松布颜色 - Modal Bottom Sheet scrim color is not shown in status bar in Jetpack compose SystemUIController 不会设置状态栏颜色 - Jetpack Compose Accompanist - SystemUIController won't set status bar color - Jetpack Compose Accompanist 是否可以将状态栏图标颜色更改为白色? 还是有深色主题状态栏? (科特林) - Is it possible to change status bar icon color to white? Or is there a dark theme status bar? (Kotlin) Jetpack Compose:即使主题较暗,文本仍为黑色 - Jetpack Compose: Text remains black even when the theme is dark 如何强制关闭 Jetpack Compose 的暗模式主题 - How to force close dark mode theme for Jetpack Compose Jetpack compose - 更改底部栏切口颜色 - Jetpack compose - change bottom bar cutout color Android Jetpack Compose(Composable) 平滑更改主题颜色 - Android Jetpack Compose(Composable) Change Theme Color Smoothly 使用 Android 和 Jetpack Compose 仅更改一个状态栏 - Change just one status bar with Android and Jetpack Compose
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM