Android 夜间模式跟随系统无法正常工作
Android Night Mode Follow System does not work correctly
我在我的应用中使用 DayNight material 主题实现了深色主题。我关注了互联网上的几篇文章和会议演讲。一切都运作良好,直到一些小事情开始发生。让我解释一下:
该应用程序有几个活动。为了不明确地为每个 activity 设置主题,我按照建议将初始主题设置放在应用程序的 onCreate() 方法中。不过这有一个缺点,我将在接下来解释。
1.) AppCompat 在 activity 级别实现夜间模式,这意味着它不会更新应用程序上下文(我用它来设置主题应用程序范围)(来源:https://issuetracker.google.com/issues/134379747)
2.) following 段代码推荐用于检查应用程序是否运行 处于哪种模式。但它 returns 与我的情况完全相反:
val currentNightMode = configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
when (currentNightMode) {
Configuration.UI_MODE_NIGHT_NO -> {} // Night mode is not active, we're using the light theme
Configuration.UI_MODE_NIGHT_YES -> {} // Night mode is active, we're using dark theme
}
3.) 将我的应用程序设置为跟随系统然后我手动切换到灯光模式(在应用程序中)然后返回跟随系统时,即使我的 phone 在system-wide 深色主题。但是,当切换我的应用主题时,它确实发生了变化。
我做错了什么?可能的解决方案是将主题设置为 activity 级别吗?
when (resources.configuration.uiMode.and(Configuration.UI_MODE_NIGHT_MASK)) {
Configuration.UI_MODE_NIGHT_NO -> themeLight.isChecked = true
Configuration.UI_MODE_NIGHT_YES -> themeDark.isChecked = true
Configuration.UI_MODE_NIGHT_UNDEFINED -> themeLight.isChecked = true
}
使用上面的代码获取当前主题。
为了立即更改主题,您需要添加
AppCompatDelegate.setDefaultNightMode(themeMode)
在您的资源文件夹中,您可以在 value_night 文件夹中添加一个 bools.xml
使用以下代码
<resources>
<bool name="is_night_mode">true</bool>
</resources>
enter image description here
并在默认文件夹中设置为 false
<resources>
<bool name="is_night_mode">false</bool>
</resources>
enter image description here
并在 class 文件中访问它
Boolean isNightTheme = context.getResources().getBoolean(R.bool.preferences_autoplay);
希望对您有所帮助。
我在我的应用中使用 DayNight material 主题实现了深色主题。我关注了互联网上的几篇文章和会议演讲。一切都运作良好,直到一些小事情开始发生。让我解释一下:
该应用程序有几个活动。为了不明确地为每个 activity 设置主题,我按照建议将初始主题设置放在应用程序的 onCreate() 方法中。不过这有一个缺点,我将在接下来解释。
1.) AppCompat 在 activity 级别实现夜间模式,这意味着它不会更新应用程序上下文(我用它来设置主题应用程序范围)(来源:https://issuetracker.google.com/issues/134379747)
2.) following 段代码推荐用于检查应用程序是否运行 处于哪种模式。但它 returns 与我的情况完全相反:
val currentNightMode = configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
when (currentNightMode) {
Configuration.UI_MODE_NIGHT_NO -> {} // Night mode is not active, we're using the light theme
Configuration.UI_MODE_NIGHT_YES -> {} // Night mode is active, we're using dark theme
}
3.) 将我的应用程序设置为跟随系统然后我手动切换到灯光模式(在应用程序中)然后返回跟随系统时,即使我的 phone 在system-wide 深色主题。但是,当切换我的应用主题时,它确实发生了变化。
我做错了什么?可能的解决方案是将主题设置为 activity 级别吗?
when (resources.configuration.uiMode.and(Configuration.UI_MODE_NIGHT_MASK)) {
Configuration.UI_MODE_NIGHT_NO -> themeLight.isChecked = true
Configuration.UI_MODE_NIGHT_YES -> themeDark.isChecked = true
Configuration.UI_MODE_NIGHT_UNDEFINED -> themeLight.isChecked = true
}
使用上面的代码获取当前主题。 为了立即更改主题,您需要添加
AppCompatDelegate.setDefaultNightMode(themeMode)
在您的资源文件夹中,您可以在 value_night 文件夹中添加一个 bools.xml 使用以下代码
<resources>
<bool name="is_night_mode">true</bool>
</resources>
enter image description here
并在默认文件夹中设置为 false
<resources>
<bool name="is_night_mode">false</bool>
</resources>
enter image description here
并在 class 文件中访问它 Boolean isNightTheme = context.getResources().getBoolean(R.bool.preferences_autoplay);
希望对您有所帮助。