1

因为我不能在 Android 2.2 中使用 PreferenceFragment,所以我想从 XML 源创建一个 ListView 并将其显示在一个普通的 Fragment 中。我有这个 XML 布局:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    <CheckBoxPreference
            android:summary="This is a CheckBox"
            android:title="CheckBox"
            android:defaultValue="false" />
</PreferenceScreen>

是否可以从中创建一个 ListView ?我认为addPreferencesFromResourcefrom的方法PreferenceActivity是一样的,但它是如何工作的?

4

1 回答 1

1

扩展 PreferenceActivity

    ...
    // fragments are not used at all and we instead, use the older 
    // PreferenceActivity APIs.

    // Add 'notifications' preferences, and a corresponding header.
    addPreferencesFromResource(R.xml.pref_general);


    // Add 'notifications' preferences, and a corresponding header.
    PreferenceCategory fakeHeader = new PreferenceCategory(this);
    fakeHeader.setTitle(R.string.pref_header_notifications);
    getPreferenceScreen().addPreference(fakeHeader);
    addPreferencesFromResource(R.xml.pref_notification);

    // Add 'data and sync' preferences, and a corresponding header.
    fakeHeader = new PreferenceCategory(this);
    fakeHeader.setTitle(R.string.pref_header_data_sync);
    getPreferenceScreen().addPreference(fakeHeader);
    addPreferencesFromResource(R.xml.pref_data_sync);

    // Bind the summaries of EditText/List/Dialog/Ringtone preferences to
    // their values. When their values change, their summaries are updated
    // to reflect the new value, per the Android Design guidelines.
    bindPreferenceSummaryToValue(findPreference("example_text"));
    bindPreferenceSummaryToValue(findPreference("example_list"));
    bindPreferenceSummaryToValue(findPreference("notifications_new_message_ringtone"));
    bindPreferenceSummaryToValue(findPreference("sync_frequency"));

pref_general.xml

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <CheckBoxPreference
        android:key="example_checkbox"
        android:title="@string/pref_title_social_recommendations"
        android:summary="@string/pref_description_social_recommendations"
        android:defaultValue="true" />

    <!-- NOTE: EditTextPreference accepts EditText attributes. -->
    <!-- NOTE: EditTextPreference's summary should be set to its value by 
        the activity code. -->
    <EditTextPreference
        android:key="example_text"
        android:title="@string/pref_title_display_name"
        android:defaultValue="@string/pref_default_display_name"
        android:selectAllOnFocus="true"
        android:inputType="textCapWords"
        android:capitalize="words"
        android:singleLine="true"
        android:maxLines="1" />

    <!-- NOTE: Hide buttons to simplify the UI. Users can touch outside the dialog to
     dismiss it. -->
    <!-- NOTE: ListPreference's summary should be set to its value by the 
    activity code. -->
    <ListPreference
        android:key="example_list"
        android:title="@string/pref_title_add_friends_to_messages"
        android:defaultValue="-1"
        android:entries="@array/pref_example_list_titles"
        android:entryValues="@array/pref_example_list_values"
        android:negativeButtonText="@null"
        android:positiveButtonText="@null" />

   </PreferenceScreen>

希望这可以帮助!

于 2013-08-07T03:49:32.987 回答