使用 android-support-v4替代 Preferences 片段

当我意识到这个库不支持 PreferenceFragments 时,我的应用程序开发突然停止了。对于一个新手 Android 开发者来说,有没有什么替代方案可以用来克服这个障碍呢?

从现在起,这是我的主窗口

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>


<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>


<FrameLayout
android:id="@+android:id/realtabcontent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>


<TabWidget
android:id="@android:id/tabs"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
/>


</TabHost>
</LinearLayout>

对于我的 TabActivity,我使用的是我在网上找到的一些东西:

public class TabControlActivity extends FragmentActivity implements TabHost.OnTabChangeListener
{
public static final int INSERT_ID = Menu.FIRST;
public static TabControlActivity thisCtx;
private TabHost mTabHost;
private HashMap mapTabInfo = new HashMap();
private TabInfo mLastTab = null;


private class TabInfo {
private String tag;
private Class clss;
private Bundle args;
private Fragment fragment;
TabInfo(String tag, Class clazz, Bundle args) {
this.tag = tag;
this.clss = clazz;
this.args = args;
}


}


class TabFactory implements TabContentFactory
{


private final Context mContext;


/**
* @param context
*/
public TabFactory(Context context) {
mContext = context;
}


/** (non-Javadoc)
* @see android.widget.TabHost.TabContentFactory#createTabContent(java.lang.String)
*/
public View createTabContent(String tag) {
View v = new View(mContext);
v.setMinimumWidth(0);
v.setMinimumHeight(0);
return v;
}


}


...*snip*...

有没有使用 android-support-v4兼容库来实现类似于偏好片段(或偏好活动)的东西?

52523 次浏览

You coul maybe use a real activity and use fragment, but I don't think it's a good choice. You should use simple PreferenceActivity and wait for improvements in retro compat libs.

You can use third party libraries such as UnifiedPreferences to use a single API for all versions of Android.

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;

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'

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.

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 .

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

As Lucius said you can use PreferenceFragmentCompat :

public class MyPreferenceFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle bundle, String s) {
PreferenceManager.setDefaultValues(getActivity(), R.xml.preferences, false);
addPreferencesFromResource(R.xml.preferences);
}
}

You must include its dependency in gradle:

dependencies {
...
compile 'com.android.support:preference-v7:23.1.X' (wherX = 0,1,...)
...
}

This way you can also can use FragmentTransaction of android.support.v4.app.FragmentTransaction and PrefernceFragment. However, you maybe have problems with themes. If it is the case, you can solve it having into account this post:

Solution to make the lib work on API 7+ while keeping material theme on API 14+

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