在PreferenceFragment Android中创建xml标头

时间:2013-07-09 11:06:30

标签: android android-fragments android-preferences

我有一个带导航抽屉菜单的应用程序。当我点击菜单中的某个项目时,我想显示一个设置片段。

当我点击该项目时,它会调用:

Fragment fragment = new SettingsFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

调用SettingsFragment:

import java.util.List;

import com.rss.R;
import android.os.Bundle;
import android.preference.PreferenceActivity.Header;
import android.preference.PreferenceFragment;


public class SettingsFragment extends PreferenceFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.preferences);
    }

    public void onBuildHeaders(List<Header> target) {
        loadHeadersFromResource(R.xml.preferences, target);
    }

}

问题是我有这个错误:

The method loadHeadersFromResource(int, List<PreferenceActivity.Header>) is undefined for the type SettingsFragment

似乎我不能将loadHeaderFromResources与PreferenceFragment一起使用,而我必须改为使用PreferenceActivity?

以下是我的偏好.xml的代码:

<?xml version="1.0" encoding="utf-8"?>
<preference-headers
        xmlns:android="http://schemas.android.com/apk/res/android">

    <header android:fragment="com.rss.preferences.ExperienceUtilisateur"
            android:icon="@drawable/ic_action_users"
            android:title="User experience"
            android:summary="Manage notifications" >
        <extra android:name="someKey" android:value="someOtherHeaderValue" />
    </header>

    <header android:icon="@drawable/ic_action_star_10"
            android:title="Follow us"
            android:summary="Help us">
        <intent android:action="android.intent.action.VIEW" android:data="http://market.android.com/details?id=com.app.mobideals" />
    </header>

</preference-headers>

1 个答案:

答案 0 :(得分:0)

标题仅用于PreferenceActivity而不是PreferenceFragment