免责声明:我已经找到了解决这个问题的方法,但是由于花费了很长时间才弄清楚为什么会出现这种情况,因此我想发帖提供答案给其他人参考。
我遇到了一个奇怪的问题,就是当在夜间模式下打开我的应用程序时,有些UI以正确的夜间模式颜色加载,而有些UI则以正常颜色加载。
免责声明:我已经找到了解决这个问题的方法,但是由于花费了很长时间才弄清楚为什么会出现这种情况,因此我想发帖提供答案给其他人参考。
我遇到了一个奇怪的问题,就是当在夜间模式下打开我的应用程序时,有些UI以正确的夜间模式颜色加载,而有些UI则以正常颜色加载。
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
val nightModeEnabled = //get value from shared prefs or wherever you are storing this flag
if (nightModeEnabled) {
Timber.d("Manually instantiating WebView to avoid night mode issue.");
try {
WebView(applicationContext)
} catch (e: Exception) {
Timber.e("Got exception while trying to instantiate WebView to avoid night mode issue. Ignoring problem.", e)
}
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
}
}
}
编辑 看起来他们可能已经在Appcompat Version 1.1.0-alpha03中修复了这个问题(尽管我并没有实际尝试过) “Fixed WebView resets DayNight Resources (b/37124582)”