我正在寻找如何切换应用主题
userSettings.night_mode = [null | yes | no]
对于#1。我相信当用户更改应用程序的设置时,以下内容已足够:
userSettings.night_mode?.let {
val nightMode = if(userSettings.night_mode == yes)
AppCompatDelegate.MODE_NIGHT_YES
else
AppCompatDelegate.MODE_NIGHT_NO
AppCompatDelegate.setDefaultNightMode(userSettings_night_mode)
}
并检查应用当前处于哪种模式:
int currentNightMode = getResources().getConfiguration().uiMode
& Configuration.UI_MODE_NIGHT_MASK
但是对于#2来说,尽管有一个MODE_NIGHT_FOLLOW_SYSTEM
并且可以做到,但还没有找到一种可靠的方法来处理系统
if (userSettings.night_mode == null) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
}
其中uses the system's night mode setting to determine if it is night or not.,但似乎每个Chris Banes
Android Q onwards has a system night mode which can be enabled in the Settings app.
Android Pie also has a system night mode setting, but it is only surfaced in the device’s ‘developer options’.
For ease, I recommend treating Android Pie the same as previous versions of Android.
那么对于Android Q之前的操作系统,如何使应用程序的夜间模式遵循系统的夜间模式设置?
(用户可以在Q之前的装有Android操作系统的设备上设置night mode
吗?)