PreferenceActivity()
is deprecated, but PreferenceFragment()
is now as well. PreferenceFragmentCompat()
is now the way to go:
Add dependency
implementation "androidx.preference:preference:1.1.1"
Or in case you are still using the support library:
implementation "com.android.support:preference-v7:28.0.0"
Extend PreferenceFragmentCompat
class MyPreferenceFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
addPreferencesFromResource(R.xml.app_preferences)
}
}
Show your Fragment
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
supportFragmentManager.beginTransaction().replace(android.R.id.content, MyPreferenceFragment()).commit()
}
Specify preferenceTheme
In your AppTheme, add either of the following preference themes, depending of which one you think looks better:
<item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
<item name="preferenceTheme">@style/PreferenceThemeOverlay.v14.Material</item>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…