gpt4 book ai didi

android - 从 XML 资源生成 ListViewItems

转载 作者:行者123 更新时间:2023-11-30 03:27:36 24 4
gpt4 key购买 nike

因为我不能在 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?我认为 PreferenceActivity 中的方法 addPreferencesFromResource 的作用相同,但它是如何工作的?

最佳答案

扩展 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>

希望对您有所帮助!

关于android - 从 XML 资源生成 ListViewItems,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17993528/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com