【问题标题】:My ListPreference is not changing App Theme during runtime我的 ListPreference 在运行时不会更改应用程序主题
【发布时间】:2020-02-04 19:12:04
【问题描述】:

当我在ListPreference 中选择一个项目来更改我的应用程序的主题时,如何让我的Activity 重新创建自己并保持特定主题?出于某种原因,无论选择 ListPreference 中的哪个项目,我的应用都停留在浅色主题上。我不确定我在这里做错了什么。

styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light"/>

    <style name="MyDarkSettingsTheme" parent="Theme.AppCompat"/>

    <style name="MyLightSettingsTheme" parent="Theme.AppCompat.Light"/>

</resources>

活动

class SettingsActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceChangeListener {
    // Declaring initial value for applying appropriate Theme
    private var mCurrentValue: Boolean = true // True is the default value

    override fun onCreate(savedInstanceState: Bundle?) {
        // Checking which Theme should be used. IMPORTANT: applying Themes MUST called BEFORE super.onCreate() and setContentView!!!
        val mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
        mCurrentValue = mSharedPreferences.getBoolean("light", true)
        if (mCurrentValue) setTheme(R.style.MyLightSettingsTheme)
        else setTheme(R.style.MyDarkSettingsTheme)

        super.onCreate(savedInstanceState)
        setContentView(R.layout.settings_activity)
        supportFragmentManager
            .beginTransaction()
            .replace(R.id.settings, SettingsFragment())
            .commit()

        val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
        sharedPreferences.registerOnSharedPreferenceChangeListener(this)
    }

    // In order to recreate Activity, we must check the value here. Because, when we come back from another Activity, the onCreate isn't called again.
    override fun onStart() {
        super.onStart()

        setContentView(R.layout.settings_activity)
        supportFragmentManager
            .beginTransaction()
            .replace(R.id.settings, SettingsFragment())
            .commit()

        val mFrameLayout = findViewById<FrameLayout>(R.id.settings)

        val mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
        val mNewValue = mSharedPreferences.getBoolean("light", true)
        // If value differs from previous Theme, recreate Activity
        if (mCurrentValue != mNewValue) recreate()

//        if (mNewValue) {
//            mFrameLayout.setBackgroundColor(Color.WHITE)
//        }
//        else {
//            mFrameLayout.setBackgroundColor(Color.BLACK)
//        }

        // ... do other stuff here

    }

    class SettingsFragment : PreferenceFragmentCompat() {
        override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
            setPreferencesFromResource(R.xml.root_preferences, rootKey)
        }
    }

    override fun onDestroy() {
        val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
        sharedPreferences.unregisterOnSharedPreferenceChangeListener(this)
        //...
        super.onDestroy()
    }

    override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) {
        when (key) {
            "list_theme" -> {
                if (sharedPreferences.getString(key, "light") == "light") {
                    setTheme(R.style.MyLightSettingsTheme)
                    recreate()
                }
                else if (sharedPreferences.getString(key, "dark") == "dark") {
                    setTheme(R.style.MyDarkSettingsTheme)
                    recreate()
                }
            }
        }
    }
}

1) 活动打开

2) ListPreference 点击

3) 选择深色偏好

MainActivity.kt

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }


    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return when (item.itemId) {
            R.id.action_settings -> {
                val intentSettings = Intent(this, SettingsActivity ::class.java)
                startActivity(intentSettings)
                true
            }

            else ->
                super.onOptionsItemSelected(item)
        }
    }
}

【问题讨论】:

    标签: android sharedpreferences android-theme android-preferences


    【解决方案1】:

    首先,您需要在 styles.xml 文件中添加更多颜色/主题。

    例子:

    <!-- Base Light application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    
    <!--Dark Theme Default-->
    <style name="AppThemeDark" parent="Theme.AppCompat">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    

    在我的 Base Application 类中,我添加了这 3 个方法。我的应用程序有 3 个主题,如果您有更多主题,您可以相应地编辑代码。

    @Override
    public void onCreate() {
        super.onCreate();
        PreferenceManager.setDefaultValues(this, R.xml.pref_main, false);
        Context context = this;
        context.setTheme(getUserTheme());
        mInstance = this;
        res = getResources();
        shared = PreferenceManager.getDefaultSharedPreferences(context);
    }
    
    public static int getTheme(Context context, int light, int dark, int amoled) {
        String darkTheme = context.getString(R.string.dark_mode_theme);
        String amoledTheme = context.getString(R.string.amoled_mode_theme);
        shared = PreferenceManager.getDefaultSharedPreferences(context);
        String theme = shared.getString(Constants.PREFERENCE_THEME, darkTheme);
        //Able to access these values outside the class!
        if (theme.equals(darkTheme)) {
            return dark;
        } else if (theme.equals(amoledTheme)) {
            return amoled;
        } else {
            return light;
        }
    }
    
    private int getUserTheme() {
        return getTheme(this, R.style.AppTheme, R.style.AppThemeDark, R.style.AppThemeAMOLED);
    }
    

    如果您没有扩展 Application 的类,请创建它,然后将此行放入 &lt;Application&gt; 标记中,然后将其添加到 AndroidMainfest

     android:name="App"
    

    我还在Base Activity Class 中添加了以下方法。我所有的活动都扩展了这门课。如果您的应用程序只有一个活动,那么这与您的相关性较低。

    protected abstract int getAppTheme(); //has to be implemented in anything that extends this class
    
    protected void setAppTheme(int id) {
        super.setTheme(id);
        themeId = id;
    }
    
     protected void onResume() {
            super.onResume()
            if (themeId !== getAppTheme())
            {
                recreate();
            }
        }
    

    更改主题后,您需要重新创建活动以激活更改。

    recreate();
    

    很遗憾,您不能从静态方法调用它。我还建议检查 onCreate 中的主题,如果当前主题与用户选择的主题不匹配,则更改主题,然后调用 recreate();。

    接下来,在扩展基本活动类的常规活动中,我添加了这个。确保在任何地方都保持相同的顺序。否则,主题将不匹配。

     public int getAppTheme() {
            return App.getTheme(this, R.style.AppTheme, R.style.AppThemeDark, R.style.AppThemeAMOLED);
        }
    

    【讨论】:

    • 我已经在 onSharedPreferenceChanged 方法中做到了。
    • 我更新了答案,请附上 MainActivity 中的代码
    • @MacaronLover 我已经编辑了我的答案,请看一下。
    • 你从哪里得到themeId?是否需要在 onCreate 某处声明?
    • 我以它为例,在我的应用程序中,我将它设置在应用程序类中,然后将其作为共享首选项检索。在你的情况下,它似乎有点太先进了。我会为您的用例看一个这样的教程:developer.android.com/guide/topics/ui/look-and-feel/darktheme
    猜你喜欢
    • 2020-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-28
    相关资源
    最近更新 更多