android - 无法切换到任何主题,如深色、浅色、系统等
问题描述
即使更改选项也可以正常工作,我也无法切换主题,sharedpreferences 为最后选择的主题保留了正确的值。
我的应用程序.kt
class MyApp : Application() {
override fun onCreate() {
super.onCreate()
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.getDefaultNightMode())
}
}
主题.xml
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:navigationBarColor">@android:color/transparent</item>
</style>
MainActivity.kt
binding.themeGroup.setOnCheckedChangeListener { _, checkedId ->
when (checkedId) {
R.id.themeLight -> setTheme(AppCompatDelegate.MODE_NIGHT_NO, THEME_LIGHT)
R.id.themeDark -> setTheme(AppCompatDelegate.MODE_NIGHT_YES, THEME_DARK)
R.id.themeBattery -> setTheme(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY, THEME_BATTERY)
R.id.themeSystem -> setTheme(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM, THEME_SYSTEM)
}
}
private fun setTheme(themeMode: Int, prefsMode: Int) {
AppCompatDelegate.setDefaultNightMode(themeMode)
storeTheme(prefsMode)
//activity?.recreate() // no luck w/o commenting
}
我有 2 个文件夹,values
和values-night
. 每个都有各自的颜色。theme.xml
里面只有 1 个values
文件夹。
解决方案
推荐阅读
- pine-script - 如何修复“无法使用参数调用绘图”错误
- android - AOSP:无法运行模拟器
- python - 什么被认为是一个好的剪影分数?
- ruby - 创建新对象时出现 Ruby 错误:参数数量错误(给定 5,预期为 0)
- python - 如何在 Altair 分面图中保持区域着色一致?
- php - 得到通知:未定义的索引:使用 json_decode
- html - 在文本中间插入文本框的 html 标签和语法是什么?
- android - 未调用 RecyclerView 适配器方法
- tweepy - 如何在日期限制内分析推文?
- python - 当我尝试将新产品添加到 Django 管理站点中的 PRODUCTS 组时,它给了我一个没有这样的表:main.auth_user__old 错误