Alternatives to PreferenceFragment with android-support-v4

后端 未结 9 2057
误落风尘
误落风尘 2020-11-28 04:50

I\'ve come to a sudden halt in the development of my app as I realized that PreferenceFragments weren\'t supported in this library. Are there any alternatives that a rookie

相关标签:
9条回答
  • 2020-11-28 04:59

    Preferences Support Library: Preference Fragments for API 7+, no matter the Activity

    A simple implementation would include a PreferenceFragmentCompat such as:

    public class PreferencesFragment extends PreferenceFragmentCompat {
        @Override
        public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
            addPreferencesFromResource(R.xml.preferences);
        }
    }
    

    You’ll also need to set preferenceTheme in your theme:

    <style name="AppTheme" parent="@style/Theme.AppCompat.Light">
      <item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
    </style>
    

    And add this in dependencies: http://developer.android.com/tools/support-library/features.html

    com.android.support:preference-v14:23.1.0
    
    0 讨论(0)
  • 2020-11-28 05:03

    You can use my own PreferenceFragment.

    It's simple and I had no issues with it so far. I think you can only display one of them at a time in a single Activity, which should be OK.

    0 讨论(0)
  • 2020-11-28 05:04

    You can extend from PreferenceActivity instead, and if you wish to have an ActionBar, you can add it using a Toolbar as being above the ListView used for the preferences.

    This can be done by using a vertical LinearLayout which holds the Toolbar and a ListView with android:id="@android:id/list" .

    If you wish, you can see my solution here .

    0 讨论(0)
  • 2020-11-28 05:10

    Important Update: The latest revision of the v7 support library now has a native PreferenceFragmentCompat.

    I am using this library, which has an AAR in mavenCentral so you can easily include it if you are using Gradle.

    compile 'com.github.machinarius:preferencefragment:0.1.1'

    0 讨论(0)
  • 2020-11-28 05:14

    UPDATE - 6/11/2015

    Support-v7 library now includes PreferenceFragmentCompat. So it will be a better idea to use it.


    Add the following project as a library project to your application.

    https://github.com/kolavar/android-support-v4-preferencefragment

    You can keep everything including your fragment transaction as it is. When importing the PreferenceFragment class, make sure the correct import header is user.

    import android.support.v4.preference.PreferenceFragment;
    

    instead of

    import android.preference.PreferenceFragment;
    
    0 讨论(0)
  • 2020-11-28 05:15

    I know this is an old question, but you can now use PreferenceFragmentCompat from com.android.support:preference-v7:23.3.0

    0 讨论(0)
提交回复
热议问题