gpt4 book ai didi

android - AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM 不工作

转载 作者:行者123 更新时间:2023-12-04 13:32:39 24 4
gpt4 key购买 nike

编辑 : 在底部更新
我在应用程序的 onCreate 中调用它: AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)然后,当我进入设备设置(设置 -> 显示 -> 夜间模式(开关:开/关))时,我将恢复我的应用程序,但未应用主题。无论我是在设备设置中打开还是关闭夜间模式,都不会应用主题。
我还添加了一个断点,并检查了以下是否返回了我 false即使从设备的设置中开启了暗模式(注意:应用程序是在暗模式关闭的情况下启动的)。

fun isNightMode(app: Application): Boolean {
return when(app.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) {
Configuration.UI_MODE_NIGHT_NO -> false
Configuration.UI_MODE_NIGHT_YES -> true
else -> false
}
}
它看起来像 application's当我从设备设置更改主题时,资源没有得到更新。
出于调试目的,我在 Application 类中覆盖了以下函数:
override fun onConfigurationChanged(newConfig: Configuration?) {
super.onConfigurationChanged(newConfig)
}
它被调用了。
编辑:看起来这是导致问题的原因。在 Application 类中有这个:
override fun attachBaseContext(base: Context) {
val locale = Locale("de", "DE")

Locale.setDefault(locale)

val resources = base.resources
val configuration = Configuration(resources.configuration)

configuration.setLocale(locale)
val baseContext = base.createConfigurationContext(configuration)

super.attachBaseContext(baseContext)
}
如果我删除上面的代码,它就可以工作。

最佳答案

只是在这里提供一个独立的答案。
感谢@Zbarcea Christian 和@Prashanth。
我的问题是一样的:覆盖 attachBaseContext()方法。所以修复它是这样的:

override fun attachBaseContext(cxt: Context) {
val resources = cxt.resources
val configuration = Configuration(resources.configuration)
// Required for the day/night theme to work
configuration.uiMode = Configuration.UI_MODE_NIGHT_UNDEFINED
val baseContext = cxt.createConfigurationContext(configuration)

// set locale and so on ...

super.attachBaseContext(baseContext)
}

关于android - AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64168632/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com