Android設置選項開發及自定義Preference樣式

  一個完整的Android應用程序都應該提供選項(或者叫偏好設置等等)讓用戶對APP的表現形式可以進行設置,好比說是否加入用戶體驗計劃,或者是否自動升級、定時提醒、開啓自啓動、後臺運行等等。提供一個好的設置項,會大大提高APP的用戶體驗。爲了完成這樣的功能,你沒必要從頭開始寫Activity或者Fragment,由於Android已經提供了實現這個功能的API,而且會自動將用戶設置以鍵值對的形式存入SharedPreference(Android的四大存儲方式之一)中。在3.0之前的系統,使用PreferenceActivity,這個類在api level 11(即Android 3.0)之後的api中丟棄,改用PreferenceFragment。二者的使用方式及函數調用大同小異,能夠根據app的目標系統版本本身去衡量。本文主要說明兩個問題,層次較淺,重在總結和說明基本用法,懂的直接飄過吧。android

1. 爲APP添加設置選項api

  Android平臺上,爲應用添加設置選項是個很是容易的事兒。這裏以PreferenceFragment爲例進行演示,畢竟時代向前發展嘛。PreferenceFragment的父類是Fragment類,而Fragment對象必須嵌入到Activity中顯示出來。由此能夠肯定思路,爲設置新建一個activity,而後將PreferenceFragment子類對象嵌入到其中,基本上就實現了選項設置,由於數據的保存與更新自動進行。app

  思路很是簡單,仍是貼下主要代碼,順便整理下思路,幫助理解。eclipse

  首先爲設置選項設置新建一個Preference配置文件,跟layout文件也是XML文件格式,層次化清晰,注意它存儲在res/xml下,而不是res/layout。系統也提供了一些比較經常使用了設置選項,好比PreferenceScreen,PreferenceCategory,CheckBoxPreference,EditTextPreferece,ListPreference等,若是須要你也能夠很方便的實現自定義的Preference,下文將會介紹實現方法。如今新建一個Preference,命名settings.xml(更傳統的命名爲preference.xml)。ide

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:preference="http://schemas.android.com/apk/res/com.test.mytest"
 4     android:title="設置" >
 5 
 6     <PreferenceScreen
 7         android:title="關於" >
 8         <Preference android:title="意見反饋" >
 9         </Preference>
10 
11         <com.test.mytest.PreferenceWithTip
12             preference:tipstring=">"
13             preference:titlestring="自定義測試" >
14             <intent
15                 android:action="android.intent.action.VIEW"
16                 android:data="http://www.baidu.com" />
17         </com.test.mytest.PreferenceWithTip>
18 
19         <Preference android:title="常見問題" >
20         </Preference>
21         <Preference android:title="檢查更新" >
22         </Preference>
23         <Preference android:title="版權聲明" >
24         </Preference>
25 
26         <SwitchPreference
27             android:key="setting_test"
28             android:title="測試一下" />
29     </PreferenceScreen>
30 
31 </PreferenceScreen>

 

  而後爲設置選項新建一個Acitivity,由於此處PreferenceFragment子類寫的很是簡單,順便之內部類實現了。函數

 1 public class SettingsActivity extends Activity {
 2 
 3     @Override
 4     public void onCreate(Bundle savedInstanceState) {
 5         super.onCreate(savedInstanceState);
 6         setContentView(R.layout.settings);
 7         setTitle("選項設置");
 8         getFragmentManager().beginTransaction().replace(R.id.settings_content, 
 9                 new PrefsFragment()).commit();
10     }
11     
12     public static class PrefsFragment extends PreferenceFragment{
13 
14         @Override
15         public void onCreate(Bundle savedInstanceState) {
16             super.onCreate(savedInstanceState);
17             addPreferencesFromResource(R.xml.settings);
18         }
19         
20     }
21     
22 }

  最後就差用戶點擊你設計好的設置選項了,到了這裏你應該猜到了,打開設置選項不過只是打開一個Intent而已。基本流程就到這裏,可是一個須要得到大用戶量應用的設置要比這個複雜得多,你可能還須要根據用戶的設置,當即對應用的表現作出調整,可能要實現onPreferenceTreeClick(PreferenceScreen  preferenceScreen,Preference prefence)。正如前文所述,剛接觸Preference,這裏僅僅總結基本用法。佈局

2. 在設置選項中使用自定義的Preference測試

  Preference類直接繼承於Object類。在上文的settings.xml中,定義好幾個Preference,Preference只提供簡單的文本顯示,而它的的子類CheckBoxPreference,SwitchPreference,EditTextPreference等則提供了較爲複雜的UI展現,並能夠保存用戶的設置數據,通常來講,這些子類Preference對於應用程序更加劇要。關於若是使用這些子類對象,其實很簡單,他們能夠像UI控件在Layout中的用法相似的應用在Preference定義的xml文件(上文定義的settings.xml)中,基本上使用了eclipse代碼提示功能就可使用,這些用法基礎但不是本文的說明重點。下面旨在介紹如何定義本身的Preference,先上圖看效果。this

圖一 自定義Preference展現spa

  圖一展現了Preference與自定義Preference樣式差異,你或許注意到第二項」自定義測試「與其餘的Preference只有一個「>「符號的差異,其實這裏包含了自定義一個Preference的完整步驟。說道這裏,順便說下,其實自定義Preference與自定義控件的方法和套路幾乎一致。仍是總結下基本步驟。

  1) 定義屬性值 attr.xml

1 <?xml version="1.0" encoding="utf-8"?>
2 <resources>
3     <declare-styleable name="PreferenceWithTip">
4         <attr name="tipstring" format="string"></attr>
5         <attr name="titlestring" format="string"></attr>
6     </declare-styleable>
7 </resources>

 

  2) 設計自定義Preference的佈局 preferencewithtip.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="horizontal"
 6     android:paddingLeft="8dp"
 7     android:paddingRight="15dp"
 8     android:paddingTop="20dp"
 9     android:paddingBottom="20dp">
10     <TextView 
11         android:id="@+id/prefs_title"
12         android:layout_width="0dp"
13         android:layout_height="wrap_content"
14         android:layout_gravity="left"
15         android:gravity="left|center_vertical"
16         android:textSize="18sp"
17         android:layout_weight="1"/>
18     <TextView 
19         android:id="@+id/prefs_tip"
20         android:layout_width="0dp"
21         android:layout_height="wrap_content"
22         android:layout_gravity="right"
23         android:gravity="right|center_vertical"
24         android:textSize="18sp"
25         android:layout_weight="1"/>
26 
27 </LinearLayout>
View Code

  3) 繼承Preference,實現本身的Preference類 PreferenceWithTip

 1 public class PreferenceWithTip extends Preference {
 2     private static final String TAG = "PreferenceWithTip";
 3     String pTitle = null;
 4     String tipstring = null;
 5     
 6     @SuppressLint("Recycle")
 7     public PreferenceWithTip(Context context, AttributeSet attrs, int defStyle) {
 8         super(context, attrs, defStyle);
 9         // 獲取自定義參數
10         Log.i(TAG,"PreferenceWithTip invoked");
11         TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.PreferenceWithTip);
12         tipstring = ta.getString(R.styleable.PreferenceWithTip_tipstring);
13         pTitle = ta.getString(R.styleable.PreferenceWithTip_titlestring);
14         ta.recycle();
15     }
16 
17     public PreferenceWithTip(Context context, AttributeSet attrs) {
18         this(context, attrs, 0);
19     }
20 
21     @Override
22     protected void onBindView(View view) {
23         super.onBindView(view);
24         TextView pTitleView = (TextView)view.findViewById(R.id.prefs_title);
25         pTitleView.setText(pTitle);
26         TextView pTipView = (TextView)view.findViewById(R.id.prefs_tip);
27         pTipView.setText(tipstring);
28     }
29 
30     @Override
31     protected View onCreateView(ViewGroup parent) {
32         return LayoutInflater.from(getContext()).inflate(R.layout.preferencewithtip,
33                 parent, false);
34     }
35     
36     //如需更新、保存數據則須要繼續編寫
37     
38 }
View Code

  4) 調用。調用代碼在文章的開頭部分已經貼出,主要代碼以下,preference是自定義的包名。

1             <com.ict.customview.PreferenceWithTip
2                 preference:tipstring=">"
3                 preference:titlestring="自定義測試" >
4                 <intent
5                     android:action="android.intent.action.VIEW"
6                     android:data="http://www.baidu.com" />
7             </com.ict.customview.PreferenceWithTip>

 

  總結一下Preference的使用仍是比較簡單的,自定義Preference也比較方便。可是要設計出一個漂亮的、人性化的Preference仍是不那麼容易,但這些都是提升用戶體驗的途徑,值得進一步挖掘。

相關文章
相關標籤/搜索