在使用PreferenceActivity的時候,布局檔案的格式一般是這樣的:
[html]
view plaincopyprint?
- <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>
<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?
- <?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 at
-
- http://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>
<?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?
- <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>
<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
當然可能還有其他的更好的方法,希望和大家多多交流