android:修改preference中view屬性

來源:互聯網
上載者:User

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,就可以看到第一種方法一樣的效果了。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.