即使没有设置暗色模式,应用程序也会引用 values-night 文件夹中的值。
values-night 文件夹中的 colors.xml 文件。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="application_background_color">#000000</color>
</resources> <!-- $Id$ -->
value文件夹中的color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="application_background_color">#FFFFFF</color>
</resources> <!-- $Id$ -->
但这并不是每次都能重现。它是间歇性地重现。
使用
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)
}
}
}