Android中的强制黑暗模式没有响应

问题描述 投票:0回答:1

我正在尝试实现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);
}
android android-theme
1个回答
0
投票
尝试以下我使用的暗模式代码。
© www.soinside.com 2019 - 2024. All rights reserved.