How do I create custom preferences using android.support.v7.preference library?

后端 未结 3 1966
忘了有多久
忘了有多久 2020-12-08 05:00

I want to support at least api 10, I want to be able to style my preferences nicely, I want to be able to have headers (or to show PreferenceScreens). It seems

3条回答
  •  北海茫月
    2020-12-08 05:30

    There is a good tutorial and Github project that explains in detail how to make a custom preference class that extends the Support Preference library:

    • https://medium.com/@JakobUlbrich/building-a-settings-screen-for-android-part-3-ae9793fd31ec -- "Building an Android Settings Screen (Part 3)", by Jakob Ulbrich

    • https://github.com/jakobulbrich/preferences-demo -- Sample Android project on Github

    The key points are:

    • You will need a custom DialogPreference or ListPreference, which controls how the preference row looks and functions. (It can also contain a reference to a layout that should display in the launched dialog). Add this DialogPreference to your XML preference file.

    • You will need a custom PreferenceDialogFragmentCompat, which controls the launching of the Dialog when the preference row is clicked. You can configure the Dialog's view in onBindDialogView().

    • In your preference screen which extends PreferenceFragmentCompat, override onDisplayPreferenceDialog() to launch your custom PreferenceDialogFragmentCompat.

    • You must only extend the support classes, not the platform classes. For example, extend androidx.preference.EditTextPreference instead of android.preference.EditTextPreference

提交回复
热议问题