我正在尝试实现here中所述的深色主题。因此,我创建了arrays.xml
为:
<resources>
<array name="themes_labels">
<item>"Default"</item>
<item>"Light"</item>
<item>"Dark"</item>
</array>
<string-array name="themes_color">
<item>"Default"</item>
<item>"Light"</item>
<item>"Dark"</item>
</string-array>
</resources>
这是我的代码,用于更改主题,该主题无效,即,主题未更改,尽管第一个Toast给出了正确的值,而第二个Toast在切换之前给出了一些神秘的值,例如16/32等。
我知道,我的值数组是键,在这里我试图读取开关中的一些int值。但是我不知道该怎么做。
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
String theme = sharedPref.getString("theme", "Default");
Toast.makeText(this, theme, Toast.LENGTH_LONG).show();
int currentNightMode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
Toast.makeText(this, ""+currentNightMode, Toast.LENGTH_LONG).show();
switch (currentNightMode) {
case Configuration.UI_MODE_NIGHT_NO:
// Night mode is not active, we're using the light theme
break;
case Configuration.UI_MODE_NIGHT_YES:
// Night mode is active, we're using dark theme
break;
default:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
}