更改主题并重新创建活动时,PreferenceFragmentCompat不会更改颜色

时间:2018-12-25 09:25:52

标签: java android kotlin android-appcompat android-preferences

我正在使用PreferenceFragmentCompat作为首选项,当我使用ListPreference将主题首选项更改为黑暗主题并重新创建活动文本时,它保持黑暗并且不可见。如果我重新打开该片段,文本将变成白色,但是在方向改变时会变成黑色

这是我的风格:

     <style name="AppThemeLight" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="preferenceTheme">@style/preferenceLight</item>

    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
    <item name="windowActionModeOverlay">true</item>

    <item name="android:textColorPrimary">@color/primaryText</item>
    <item name="android:textColorSecondary">@color/secondaryText</item>

</style>

<style name="AppThemeDark" parent="Theme.AppCompat.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="preferenceTheme">@style/preferenceDark</item>

    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
    <item name="windowActionModeOverlay">true</item>

    <item name="android:windowBackground">@color/primaryText</item>

    <item name="android:textColorPrimary">@color/primaryTextDark</item>
    <item name="android:textColorSecondary">@color/secondaryText</item>

</style>

<style name="preferenceLight" parent="PreferenceThemeOverlay.v14.Material">
    <item name="android:textColor">@color/primaryText</item>
    <item name="android:textColorPrimary">@color/primaryText</item>
    <item name="android:textColorSecondary">@color/secondaryText</item>
</style>

<style name="preferenceDark" parent="PreferenceThemeOverlay.v14.Material">
    <item name="android:textColor">@color/primaryTextDark</item>
    <item name="android:textColorPrimary">@color/primaryTextDark</item>
    <item name="android:textColorSecondary">@color/secondaryText</item>
</style>

和片段

    class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedPreferenceChangeListener{

private lateinit var advertisePreferences: AdvertisePreferences

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    advertisePreferences = AdvertisePreferences(context!!)
}

override fun onResume() {
    super.onResume()
    (activity as AppCompatActivity).supportActionBar?.title = getString(R.string.settings)
    preferenceManager.sharedPreferences.registerOnSharedPreferenceChangeListener(this)
}

override fun onPause() {
    super.onPause()
    preferenceManager.sharedPreferences.unregisterOnSharedPreferenceChangeListener(this)
}

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
    setPreferencesFromResource(R.xml.app_preferences, rootKey)
}

override fun onPreferenceTreeClick(preference: Preference): Boolean {

    when(preference.key){

    }

    return super.onPreferenceTreeClick(preference)
}

override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) {

    when(key){
        getString(R.string.theme_key) -> {
            activity?.recreate()
        }
    }
}

    }

1 个答案:

答案 0 :(得分:0)

似乎是库中的错误。现在已在 androidx.preference:preference:1.1.0-alpha02

中修复