在谷歌官方的文档中,要求我们使用xml来生成设置界面,但是在国内推广的并不广泛,很多人都喜欢使用自定义嵌套布局来实现设置界面,麻烦不说,配置成iOS的样子也不伦不类的。为何不直接使用原生设置界面呢?可能不到一小时你就可以生成一个完美的设置界面。
特点:
About
在xml文件夹中新建
settings_general.xml
配置如下
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="Post">
<ListPreference "Post Limit" android:defaultValue="10" android:entryValues="@array/settings_entry_values_post_limit" android:entries="@array/settings_entries_post_limit"/>
<"Preview Image Quality" "0" android:key="preview_image" "@array/settings_entry_values_preview_image_size" "@array/settings_entries_image_size"/>
<"Download Image Quality" "2" "download_image" CheckBoxPreference "Safe Mode" "true" android:summaryOn="You are now under protected" android:summaryOff="Warning! You may need be more careful!"/>
</PreferenceCategory>
<"About">
<"Open Source" "about">
</PreferenceScreen>
<"Rate this app" android:summary="Like the app?Please consider leaving a review in the Store!">
<intent android:action="android.intent.action.VIEW" android:data="market://details?id=com.github.miao1007.kwallpaper"/>
</"Feedback" "miao1007@gmail.com">
<"mailto:miao1007@gmail.com?subject=WallpaperAppFeedback"/>
</PreferenceScreen>
</PreferenceCategory>
</PreferenceScreen>
接着是代码中的数组
settings_pref.xml
resources>
<string-array name="settings_entries_post_limit">
<item>10</item>
<item>15</item>20</item>
</string-array>
<"settings_entry_values_post_limit">
<"settings_entries_image_size">
<item>Small</item>Medium</item>Large</item>Source</"settings_entry_values_preview_image_size">
<item>0</item>1</item>2</item>3</string-array>
</resources>
AboutLibrary是一款开源美观的AboutActivity/Fragments
生成器。
Gradle配置如下
compile('com.mikepenz.aboutlibraries:library:4.6.5@aar') {
transitive = true
}
新建一个
aboutlibraries_description.xml
内容如下
string "aboutLibraries_description_name">KWallPaper</string>
<"aboutLibraries_description_showIcon">true</"aboutLibraries_description_showVersion">true</"aboutLibraries_description_text">
<![CDATA[This is a wallpaper app in material design. <br /><br /><b>You can fork me on <a href="https://github.com/miao1007/Lollipop-Animation-Sample">GitHub</a> </b>]]>
</string>
</ 3. 编写Fragment
写一个Fragment,嵌入Activity中即可。根据xml中的key配置Click/Switch事件。
PreferenceFragment fragment = new PreferenceFragment() {
@Override public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
View view = super.onCreateView(inflater,container,savedInstanceState);
//Set night-mode or other UI changes
return view;
}
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings_general);
findPreference("about").setOnPreferenceClickListener(
new Preference.OnPreferenceClickListener() {
@Override boolean onPreferenceClick(Preference preference) {
//config About Library
new Libs.Builder().withActivityTitle("About").withFields(R.string.class.getFields()).start(getActivity());
return false;
}
});
}
4. 配置的读取与修改
把这个工具类加入即可,使用时按照xml中的key作为参数即可
public class PreferenceUtils {
static String getPrefString(String key,final String defaultValue) {
final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(GlobalContext.getInstance());
return settings.getString(key,defaultValue);
}
static setPrefString(Context context,0)">final String key,0)">final String value)
{
final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
settings.edit().putString(key,value).commit();
}
getPrefBoolean(Context context,0)">final boolean defaultValue) {
final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
return settings.getBoolean(key,210)">hasKey(Context context,0)">final String key) {
return PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()).contains(key);
}
setPrefBoolean(Context context,0)">boolean value) {
final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
settings.edit().putBoolean(key,210)">setPrefInt(Context context,0)">int value) {
final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
settings.edit().putInt(key,0)">int getPrefInt(Context context,0)">int defaultValue) {
return settings.getInt(key,210)">setPrefFloat(Context context,0)">float value) {
final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
settings.edit().putFloat(key,0)">float getPrefFloat(Context context,0)">float defaultValue) {
return settings.getFloat(key,210)">setSettingLong(Context context,0)">long value) {
final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
settings.edit().putLong(key,0)">long getPrefLong(Context context,0)">long defaultValue) {
return settings.getLong(key,210)">clearPreference(Context context,0)">final SharedPreferences p) {
final SharedPreferences.Editor editor = p.edit();
editor.clear();
editor.commit();
}
}
通过编写如上代码,你就拥有了一个原生的设置界面,实例代码在Github仓库中。
如果你希望设置界面更加个性化,点这里。