[英]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.