android開發包preference提供了我們一些方面的設定工具,PreferenceActivity,Preference,PreferenceScreen,CheckBoxPreference等,利用他們可以方便的建立應用程式的屬性設定。
有些時候,我們並不需要系統給我們的這麼單調的ui,我們通常會修改一些屬性,比如view的背景,字型的屬性等,那麼這個時候有兩種選擇:
1.針對單個應用程式,定義一個cutom的layout,當然這個layout跟系統的layout元素要一致(否則你怎麼改呢?),然後在preference.xml(檔案名稱你自己隨便取)android:layout添加你定一個 的layout就可以了。
2.針對整個android系統,修改preference相關的屬性,那麼所有的用到PreferenceActivity的介面都會相應的改變。這裡只需要修改相應的系統layout檔案(在framework/base/core/res/res/layout),比如preference_category.xml等。
1.先說說地一種吧,比較常用到的。
首先,定義你的layout檔案,這個layoutn你直接到framework拷下來,這裡標記為custom_preference.xml
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 android:layout_width="match_parent" android:layout_height="wrap_content"
3 android:minHeight="?android:attr/listPreferredItemHeight"
4 android:gravity="center_vertical" android:paddingRight="?android:attr/scrollbarSize">
5
6 <RelativeLayout android:layout_width="wrap_content"
7 android:layout_height="wrap_content" android:layout_marginLeft="15dip"
8 android:layout_marginRight="6dip" android:layout_marginTop="6dip"
9 android:layout_marginBottom="6dip" android:layout_weight="1">
10
11 <TextView android:id="@+android:id/title"
12 android:layout_width="wrap_content" android:layout_height="wrap_content"
13 android:singleLine="true" android:textAppearance="?android:attr/textAppearanceLarge"
14 android:ellipsize="marquee" android:fadingEdge="horizontal"
15 android:textColor="#475ad7"/>
16
17 <TextView android:id="@+android:id/summary"
18 android:layout_width="wrap_content" android:layout_height="wrap_content"
19 android:layout_below="@android:id/title" android:layout_alignLeft="@android:id/title"
20 android:textAppearance="?android:attr/textAppearanceSmall"
21 android:textColor="#5ad747"/>
22
23 </RelativeLayout>
24
25 <!-- Preference should place its actual preference widget here. -->
26 <LinearLayout android:id="@+android:id/widget_frame"
27 android:layout_width="wrap_content" android:layout_height="match_parent"
28 android:gravity="center_vertical" android:orientation="vertical"/>
29
30 </LinearLayout>
然後,你在些preference.xml檔案的時候,應用到這個layout檔案。
1 <?xml version="1.0" encoding="utf-8"?>
2 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
3 <CheckBoxPreference android:key="checkbox_key"
4 android:title="checkbox_title"
5 android:summary="checkbox_summery"
6 android:defaultValue="true"
7 android:layout="@layout/custom_preference_layout"></CheckBoxPreference>
8 </PreferenceScreen>
ok,到這裡,你就像平時一樣使用這個preference.xml檔案就好了,你會發現字型的顏色成功改變了
2.第二種情況,需要搭建android系統的編譯環境,這個環境怎麼搭建這裡就不說了。
直接到系統layout檔案(在framework/base/core/res/res/layout),修改preference.xml,這裡只是針對上面那個例子,你當然也可以修改其他的preference相應的檔案。preference.xml檔案的內容就是第一種情況的custom_preference.xml檔案的內容,這裡就不重複了。
修改後重新mka下系統,重啟模擬器,ok,就可以看到第一種方法一樣的效果了。