ListPreference在运行时未更改主题

问题描述 投票:0回答:1

[当我在Activity中选择一个项目来更改应用程序的主题时,如何获得ListPreference来重新创建自己并保持特定的主题?由于某些原因,无论选择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 value here because when we come back from another Activity onCreate doesn'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)活动已打开

enter image description here

[2)ListPreference单击

enter image description here

[3)选择黑暗偏好

enter image description here

android sharedpreferences android-theme android-preferences
1个回答
-1
投票

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

recreate(); 

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

© www.soinside.com 2019 - 2024. All rights reserved.