簡體   English   中英

設置列表首選項后,為什么我的應用程序不會更改為暗模式 (Android Q)?

[英]Why won't my app change to dark mode (Android Q) when the list preference is set?

我在使用 Android 開發人員指南的設置中有一個列表首選項,但是當用戶選擇淺色或深色時,應用程序主題不會改變。

root_preferences:

<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">

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

<Preference
    app:key="webpage"
    app:title="Profile"
    app:summary="View and edit your profile (opens in browser)">
<intent
    android:action="android.intent.action.VIEW"
    android:data="https://canvas.thelatinschool.org/profile" />
</Preference>

<Preference
    android:id="@+id/notq"
    app:key="notq"
    app:title="Dark Mode"
    app:summary="Dark Mode under construction for Android Pie and below"
    >

</Preference>

數組:

<resources xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">
<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>

我的 SettingsFragment 應該更改主題:

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 == "Light") {
                    AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_NO);
                }
                if (theme == "Dark") {
                    AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_YES);
                }
                if (theme == "System default") {
                    AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_FOLLOW_SYSTEM);
                }
                return true;
            }
        });
    }
}

日志輸出給出值“Light”或“Dark”,但主題本身不會改變。 我究竟做錯了什么?

另外,我正在嘗試為 Android 9 及更低版本的深色主題實現制作另一個列表首選項,因此假設我的數組值相同,我必須在 SettingsFragment 中更改什么才能使其工作?

不要將String==進行比較。 使用if theme.equals("Light") 在 Java 中, ==比較引用標識(兩個對象存儲在相同的內存地址),而String.equals比較兩個String的值,就像人類所期望的那樣。

問題是您正在將條目值與條目名稱進行比較。 應該是這樣的:

if (theme == "light") {
    AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_NO);
}
if (theme == "dark") {
    AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_YES);
}
if (theme == "default1") {
    AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_FOLLOW_SYSTEM);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM