android 定製PreferenceScreen

來源:互聯網
上載者:User

在使用PreferenceActivity的時候,布局檔案的格式一般是這樣的:

[html]
view plaincopyprint?
  1. <PreferenceCategory  
  2.         android:title="@string/launch_preferences">  
  3.   
  4.     <!-- This PreferenceScreen tag sends the user to a new fragment of  
  5.          preferences.  If running in a large screen, they can be embedded  
  6.          inside of the overall preferences UI. -->  
  7.     <PreferenceScreen  
  8.             android:fragment="com.example.android.apis.preference.PreferenceWithHeaders$Prefs1FragmentInner"  
  9.             android:title="@string/title_fragment_preference"  
  10.             android:summary="@string/summary_fragment_preference">  
  11.         <!-- Arbitrary key/value pairs can be included for fragment arguments -->  
  12.         <extra android:name="someKey" android:value="somePrefValue" />  
  13.     </PreferenceScreen>  
  14.   
  15.     <!-- This PreferenceScreen tag sends the user to a completely different  
  16.          activity, switching out of the current preferences UI. -->  
  17.     <PreferenceScreen  
  18.             android:title="@string/title_intent_preference"  
  19.             android:summary="@string/summary_intent_preference">  
  20.   
  21.         <intent android:action="android.intent.action.VIEW"  
  22.                 android:data="http://www.android.com" />  
  23.   
  24.     </PreferenceScreen>  
  25.   
  26. </PreferenceCategory>  
    <PreferenceCategory            android:title="@string/launch_preferences">        <!-- This PreferenceScreen tag sends the user to a new fragment of             preferences.  If running in a large screen, they can be embedded             inside of the overall preferences UI. -->        <PreferenceScreen                android:fragment="com.example.android.apis.preference.PreferenceWithHeaders$Prefs1FragmentInner"                android:title="@string/title_fragment_preference"                android:summary="@string/summary_fragment_preference">            <!-- Arbitrary key/value pairs can be included for fragment arguments -->            <extra android:name="someKey" android:value="somePrefValue" />        </PreferenceScreen>        <!-- This PreferenceScreen tag sends the user to a completely different             activity, switching out of the current preferences UI. -->        <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>

但是我們不能控制其中的title和summary的字型的樣式,使用的是系統的樣式

怎麼樣修改title和summary的字型和顏色呢?

這裡主要以PreferenceScreen為例說明:

首先PreferenceScreen的布局檔案在 framework中

位置如下   /framework/base/core/res/res/layout/preference.xml具體內容如下

[html]
view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!-- Copyright (C) 2006 The Android Open Source Project  
  3.   
  4.      Licensed under the Apache License, Version 2.0 (the "License");  
  5.      you may not use this file except in compliance with the License.  
  6.      You may obtain a copy of the License at  
  7.     
  8.           http://www.apache.org/licenses/LICENSE-2.0  
  9.     
  10.      Unless required by applicable law or agreed to in writing, software  
  11.      distributed under the License is distributed on an "AS IS" BASIS,  
  12.      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  13.      See the License for the specific language governing permissions and  
  14.      limitations under the License.  
  15. -->  
  16.   
  17. <!-- Layout for a Preference in a PreferenceActivity. The  
  18.      Preference is able to place a specific widget for its particular  
  19.      type in the "widget_frame" layout. -->  
  20. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
  21.     android:layout_width="match_parent"  
  22.     android:layout_height="wrap_content"  
  23.     android:minHeight="?android:attr/listPreferredItemHeight"  
  24.     android:gravity="center_vertical"  
  25.     android:paddingRight="?android:attr/scrollbarSize">  
  26.       
  27.     <RelativeLayout  
  28.         android:layout_width="wrap_content"  
  29.         android:layout_height="wrap_content"  
  30.         android:layout_marginLeft="15dip"  
  31.         android:layout_marginRight="6dip"  
  32.         android:layout_marginTop="6dip"  
  33.         android:layout_marginBottom="6dip"  
  34.         android:layout_weight="1">  
  35.       
  36.         <TextView android:id="@+android:id/title"  
  37.             android:layout_width="wrap_content"  
  38.             android:layout_height="wrap_content"  
  39.             android:singleLine="true"  
  40.             android:textAppearance="?android:attr/textAppearanceLarge"  
  41.             android:ellipsize="marquee"  
  42.             android:fadingEdge="horizontal" />  
  43.               
  44.         <TextView android:id="@+android:id/summary"  
  45.             android:layout_width="wrap_content"  
  46.             android:layout_height="wrap_content"  
  47.             android:layout_below="@android:id/title"  
  48.             android:layout_alignLeft="@android:id/title"  
  49.             android:textAppearance="?android:attr/textAppearanceSmall"  
  50.             android:maxLines="4" />  
  51.   
  52.     </RelativeLayout>  
  53.       
  54.     <!-- Preference should place its actual preference widget here. -->  
  55.     <LinearLayout android:id="@+android:id/widget_frame"  
  56.         android:layout_width="wrap_content"  
  57.         android:layout_height="match_parent"  
  58.         android:gravity="center_vertical"  
  59.         android:orientation="vertical" />  
  60.   
  61. </LinearLayout>  
<?xml version="1.0" encoding="utf-8"?><!-- Copyright (C) 2006 The Android Open Source Project     Licensed under the Apache License, Version 2.0 (the "License");     you may not use this file except in compliance with the License.     You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0     Unless required by applicable law or agreed to in writing, software     distributed under the License is distributed on an "AS IS" BASIS,     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.     See the License for the specific language governing permissions and     limitations under the License.--><!-- Layout for a Preference in a PreferenceActivity. The     Preference is able to place a specific widget for its particular     type in the "widget_frame" layout. --><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"    android:layout_height="wrap_content"    android:minHeight="?android:attr/listPreferredItemHeight"    android:gravity="center_vertical"    android:paddingRight="?android:attr/scrollbarSize">        <RelativeLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginLeft="15dip"        android:layout_marginRight="6dip"        android:layout_marginTop="6dip"        android:layout_marginBottom="6dip"        android:layout_weight="1">            <TextView android:id="@+android:id/title"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:singleLine="true"            android:textAppearance="?android:attr/textAppearanceLarge"            android:ellipsize="marquee"            android:fadingEdge="horizontal" />                    <TextView android:id="@+android:id/summary"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_below="@android:id/title"            android:layout_alignLeft="@android:id/title"            android:textAppearance="?android:attr/textAppearanceSmall"            android:maxLines="4" />    </RelativeLayout>        <!-- Preference should place its actual preference widget here. -->    <LinearLayout android:id="@+android:id/widget_frame"        android:layout_width="wrap_content"        android:layout_height="match_parent"        android:gravity="center_vertical"        android:orientation="vertical" /></LinearLayout>

可以看到PreferenceScreen的布局檔案中主要的是兩個TextView分別顯示title和summary

我們在每個app中可以按照這個樣式重新定義PreferenceScreen的樣式,比如我們可以定義一個custom_preference.xml檔案內容和上面的差不多,只不過重新定義了其text的大小和顏色,在

[html]
view plaincopyprint?
  1. <PreferenceScreen  
  2.                android:fragment="com.example.android.apis.preference.PreferenceWithHeaders$Prefs1FragmentInner"  
  3.                android:title="@string/title_fragment_preference"  
  4.                android:summary="@string/summary_fragment_preference">  
  5.            <!-- Arbitrary key/value pairs can be included for fragment arguments -->  
  6.            <extra android:name="someKey" android:value="somePrefValue" />  
  7.        </PreferenceScreen>  
 <PreferenceScreen                android:fragment="com.example.android.apis.preference.PreferenceWithHeaders$Prefs1FragmentInner"                android:title="@string/title_fragment_preference"                android:summary="@string/summary_fragment_preference">            <!-- Arbitrary key/value pairs can be included for fragment arguments -->            <extra android:name="someKey" android:value="somePrefValue" />        </PreferenceScreen>

添加一個屬性 android:layout="@layout/custom_preference" 載入自己的定義的preference的布局檔案即可。

以此還可以 重新定義對應的 

PrefercenceCategory  樣式檔案 --------------- framework/base/core/res/res/preference_category.xml

CheckBoxPreference 樣式檔案-----------------  frameworks/base/core/res/res/layout/preference_widget_checkbox.xml 

EditTextPreference    樣式檔案-----------------frameworks/base/core/res/res/layout/preference_dialog_edittext.xml

當然可能還有其他的更好的方法,希望和大家多多交流

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.