安卓在API级别29和更高版本(https://developer.android.com/guide/topics/ui/look-and-feel/darktheme)中引入了黑暗主题。要在你自己的应用程序中支持黑暗主题,你的应用程序的主题需要从DayNight主题继承。但是,如果你做了自己的主题,Android有没有意图引起人们对系统主题变化的注意呢?
发布于 2020-04-15 15:18:20
如果将android:configChanges="uiMode"添加到清单中的活动,则当用户更改主题时,将调用onConfigurationChanged方法。如果你覆盖了它,你可以在那里做所有相关的工作。为了检查当前主题是什么,您可以执行以下操作:
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
}(source)
https://stackoverflow.com/questions/61223010
复制相似问题