0

我在列表首选项中将主题更改定义为(在 SettingsActivity 中膨胀):

 <ListPreference
        app:defaultValue="default"
        app:entries="@array/themes_labels"
        app:entryValues="@array/themes_color"
        app:key="@string/Theme"
        app:useSimpleSumm"theme"
        app:title=aryProvider="true" />
  </PreferenceCategory>

并且 arraus 定义为:

<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>

现在,虽然我尝试遵循黑暗主题开发人员指南,但我不理解变量configuration.uiMode,因此我已经实现为(在 MainActivity 中):

@Override
  protected void onCreate(Bundle savedInstanceState) {
    if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.Q) {
      AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
    } else{
      AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY);
    }
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    String theme = sharedPref.getString("theme", "Default");
//    Toast.makeText(this, theme, Toast.LENGTH_LONG).show();
    if (theme.equals("Dark")){
      AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    }else if (theme.equals("Light")){
      AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    } else{
      if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.Q) {
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
      } else{
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY);
      }
    }
    super.onCreate(savedInstanceState);
    mContext = getApplicationContext();
    Bundle bundle = new Bundle();

虽然这在模拟器上运行良好,但在我的真实设备中,它突然崩溃了。但是,由于我没有任何办法知道出了什么问题,所以我没有任何线索。

有人可以帮忙吗?

4

0 回答 0