0

我已经为用户的暗模式偏好设置了一个具有不同值的 ListPreference。现在,我只想让应用程序与 Android Q 中引入的新暗模式实现一起工作。如果我设置系统暗模式,应用程序会更改,因为我启用isforcedarkallowedstyles.xml.

设置片段:

public static class SettingsFragment extends PreferenceFragmentCompat {
    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        setPreferencesFromResource(R.xml.root_preferences, rootKey);
        PreferenceScreen screen = getPreferenceScreen();
        final ListPreference listPreference = (ListPreference) findPreference("theme");
        Preference preference = (Preference) findPreference("notq");
        int api = Integer.valueOf(android.os.Build.VERSION.SDK_INT);
        if (api > 28) {
            screen.removePreference(preference);
        }
        if (api < 29) {
            screen.removePreference(listPreference);
        }
        listPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                listPreference.setValue(newValue.toString());
                theme = String.valueOf(listPreference.getEntry());
                Log.d("debug", theme);
                if (theme.equals("Light")) {
                    AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_NO);
                }
                if (theme.equals("Dark")) {
                    AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_YES);
                }
                if (theme.equals("System default")) {
                    AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_FOLLOW_SYSTEM);
                }
                return true;
            }
        });
    }
}

Arrays.xml(暗模式首选项的值)

<resources xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Reply Preference -->
<string-array name="theme_entries">
    <item>Light</item>
    <item>Dark</item>
    <item>System default</item>

</string-array>

<string-array name="theme_values">
    <item>light</item>
    <item>dark</item>
    <item>default1</item>
</string-array>
</resources>

我的列表偏好:

 <ListPreference
    android:id="@+id/list"
    app:entries="@array/theme_entries"
    app:entryValues="@array/theme_values"
    app:key="theme"
    app:title="@string/theme_title"
    app:useSimpleSummaryProvider="true" />

我正在使用的应用程序主题(styles.xml):

 <style name="AppTheme2" parent="Theme.AppCompat.DayNight.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="android:forceDarkAllowed" tools:targetApi="q">true</item>
    <item name="android:isLightTheme" tools:targetApi="q">true</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">#FFFFFF</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:backgroundDimEnabled">false</item>
</style>

有什么建议么?

4

2 回答 2

1

styles.xmlTheme.AppCompat.DayNight.DarkActionBar启用forceDarkAllowed. 将其更改为Theme.AppCompatTheme.MaterialComponentsforceDarkAllowed当设置为 true时,您不能从昼夜主题继承。你可以在这里阅读https://developer.android.com/guide/topics/ui/look-and-feel/darktheme部分的内容

于 2020-05-06T20:12:30.470 回答
0
String theme = String.valueOf(listPreference.getEntry()); 

应该可以正常工作,您只是没有将主题声明为字符串

:)

于 2020-07-29T21:38:07.310 回答