原文连接
我们在项目中经常需要用到设置(Setting),在安卓中主要使用android.preference.PreferenceFragment
和 android.support.v7.preference.PreferenceFragmentCompat
,在Android API 28中PreferenceFragment
已经过时,推荐使用PreferenceFragmentCompat
This class was deprecated in API level 28.
Use PreferenceFragmentCompat
[图片上传失败...(image-696f07-1539755279121)]
添加依赖
implementation group: 'com.android.support', name: 'preference-v7', version: '28.0.0'
创建配置文件
app\src\main\res\xml\preference_setting.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:title="@string/root_title">
<Preference
android:key="basic_preference"
android:title="@string/title_basic_preference"
android:summary="@string/summary_basic_preference" />
<Preference
android:key="stylish_preference"
android:title="@string/title_stylish_preference"
android:summary="@string/summary_stylish_preference" />
<Preference
android:key="preference_with_icon"
android:title="Preference with icon"
android:summary="This preference has an icon"
android:icon="@android:drawable/ic_menu_camera" />
<PreferenceCategory
android:title="@string/inline_preferences">
<CheckBoxPreference
android:key="checkbox_preference"
android:title="@string/title_checkbox_preference"
android:summary="@string/summary_checkbox_preference" />
<SwitchPreference
android:key="switch_preference"
android:title="Switch preference"
android:summary="This is a switch" />
<DropDownPreference
android:key="dropdown_preference"
android:title="@string/title_dropdown_preference"
android:summary="@string/summary_dropdown_preference"
android:entries="@array/entries_list_preference"
android:entryValues="@array/entryvalues_list_preference" />
</PreferenceCategory>
<PreferenceCategory
android:title="@string/dialog_based_preferences">
<EditTextPreference
android:key="edittext_preference"
android:title="@string/title_edittext_preference"
android:summary="@string/summary_edittext_preference"
android:dialogTitle="@string/dialog_title_edittext_preference" />
<ListPreference
android:key="list_preference"
android:title="@string/title_list_preference"
android:summary="@string/summary_list_preference"
android:entries="@array/entries_list_preference"
android:entryValues="@array/entryvalues_list_preference"
android:dialogTitle="@string/dialog_title_list_preference" />
<MultiSelectListPreference
android:key="multi_select_list_preference"
android:title="@string/title_multi_list_preference"
android:summary="@string/summary_multi_list_preference"
android:entries="@array/entries_list_preference"
android:entryValues="@array/entryvalues_list_preference"
android:dialogTitle="@string/dialog_title_multi_list_preference" />
</PreferenceCategory>
<PreferenceCategory
android:title="@string/launch_preferences">
<!-- This PreferenceScreen tag serves as a screen break (similar to page break
in word processing). Like for other preference types, we assign a key
here so it is able to save and restore its instance state. -->
<PreferenceScreen
android:key="screen_preference"
android:title="@string/title_screen_preference"
android:summary="@string/summary_screen_preference">
<!-- You can place more preferences here that will be shown on the next screen. -->
<CheckBoxPreference
android:key="next_screen_checkbox_preference"
android:title="@string/title_next_screen_toggle_preference"
android:summary="@string/summary_next_screen_toggle_preference" />
</PreferenceScreen>
<PreferenceScreen
android:title="@string/title_intent_preference"
android:summary="@string/summary_intent_preference">
<intent android:action="android.intent.action.VIEW"
android:data="http://www.android.com" />
</PreferenceScreen>
</PreferenceCategory>
<PreferenceCategory
android:title="@string/preference_attributes">
<CheckBoxPreference
android:key="parent_checkbox_preference"
android:title="@string/title_parent_preference"
android:summary="@string/summary_parent_preference" />
<!-- The visual style of a child is defined by this styled theme attribute. -->
<CheckBoxPreference
android:key="child_checkbox_preference"
android:dependency="parent_checkbox_preference"
android:layout="?android:attr/preferenceLayoutChild"
android:title="@string/title_child_preference"
android:summary="@string/summary_child_preference" />
</PreferenceCategory>
</PreferenceScreen>
创建SettingFragment
使用addPreferencesFromResource
方法添加上一部添加的方法
public class SettingsFragment extends PreferenceFragmentCompat{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preference_setting.xml);
}
}
保存与检索
PreferenceFragmentCompat
自动保存用户选择的配置信息,然后我们可以通过getDefaultSharedPreferences(android.content.Context)
读取键值对配置信息
参考连接
https://developer.android.com/reference/android/support/v7/preference/PreferenceFragmentCompat
https://developer.android.com/reference/android/preference/PreferenceFragment
网友评论