Android API等级30的setSystemBarsAppearance无法覆盖主题数据。

18

在 Android 11 之前 (API 级别 30),我在我的主题中设置了<item name="android:windowLightStatusBar">true</item>,并在需要时在代码中进行了额外更改。

fun setLightStatusBar(){ 
    window?.decorView?.let { it.systemUiVisibility = it.systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR }
} 

fun setDarkStatusBar(){
    window?.decorView?.let { it.systemUiVisibility = it.systemUiVisibility and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv() }
}

然而,Android-30新增了一种控制方式

fun setLightStatusBar(){ 
    window?.insetsController?.setSystemBarsAppearance(APPEARANCE_LIGHT_STATUS_BARS, APPEARANCE_LIGHT_STATUS_BARS)
} 

fun setDarkStatusBar(){
    window?.insetsController?.setSystemBarsAppearance(0, APPEARANCE_LIGHT_STATUS_BARS)
}

但我的问题是,这不能覆盖主题设置的值,因此我需要使用样式或代码来完成全部工作。

我的问题是,这是否有意为之,还是我在某个地方漏掉了什么?


疯狂的机器人。它也不工作,我只能使用废弃的 API,但一切都能正常运行(没有其他选择)¯_(ツ)_/¯。 - bitvale
刚刚检查了一下,它适用于API 31,但不适用于API 30。 - Buntupana
2个回答

3

我从 styles.xml 中删除了 <item name="android:windowLightStatusBar">true/false</item>,然后它正确地工作了。


1
对我来说不起作用,标志没有被清除为深色状态栏。 - AndroidRuntimeException

3
如果您在主题中设置了android:windowLightStatusBartrue,则需要执行已弃用的调用来删除系统UI标志以使其起作用。
activity.window.decorView.run {
    systemUiVisibility =
        systemUiVisibility and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv()
}

似乎WindowInsetsControllersystemUiVisibility都可以控制状态栏主题,但是它们使用的机制不同。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接