Android记住黑暗主题

时间:2020-08-01 18:33:37

标签: java android sharedpreferences android-dark-theme android-darkmode

我正在尝试为我的应用创建一个深色主题。到目前为止一切顺利,但是当我关闭应用程序时,该应用程序不记得设置的黑暗主题。

我通过以下方法更改深色主题:

    public static void setDarkMode(Object value, SharedPreferences preferences) {
        SharedPreferences.Editor editor = preferences.edit();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            if (value.equals("on")) {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                editor.putString("dark_theme", (String) value);
                editor.apply();
            } else if (value.equals("off")) {
                Log.i(TAG, "off");
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                editor.putString("dark_theme", (String) value);
                editor.apply();
            } else if (value.equals("follow_system")) {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
                editor.putString("dark_theme", (String) value);
                editor.apply();
            } else {
                Log.e(TAG, "Dark Mode Preferences android Q+ did not give the right value");
            }
        } else {
            if ((boolean) value) {
                Log.i(TAG, "Set night mode on");
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                editor.putBoolean("dark_theme", (boolean) value);
                editor.apply();
            } else {
                Log.i(TAG, "set night mode off");
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                editor.putBoolean("dark_theme", (boolean) value);
                editor.apply();
            }
        }
    }

在MainActivity中,我尝试通过调用方法setDayNightMode()来设置模式,但这总是设置MODE_NIGHT_FOLLOW_SYSTEM

    private void setDayNightMode() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            Log.i(TAG, "Build version >= Q");
            String setting = settings.getString("dark_theme", "follow_system");
            if (setting.equals(getResources().getString(R.string.on))) {
                Log.i(TAG, "night mode on");
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
            } else if (setting.equals(getResources().getString(R.string.off))) {
                Log.i(TAG, "Night mode off");
                AppCompatDelegate.setDefaultNightMode((AppCompatDelegate.MODE_NIGHT_NO));
            } else if (setting.equals(getResources().getString(R.string.follow_sys))) {
                Log.i(TAG, "Night mode follow system");
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
            }
        } else {
            Boolean setting = settings.getBoolean("dark_theme", false);
            if(setting) {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
            } else if (!setting) {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
            }
        }
    }

似乎SharedPreferences settings定义的this.getSharedPreferences(getResources().getString(R.string.dark_theme_preference_key), MODE_PRIVATE)确实返回了follow_system值。您知道如何记住应用程序中的黑暗模式吗?

1 个答案:

答案 0 :(得分:0)

当用户在设置中更改应用的主题时,请使用Shared Preferences。然后,当用户启动应用程序时,在onCreate的主要活动中,加载键值对并根据用户保存的选择设置主题

相关问题