在使用PreferenceActivity的时候,布局文件的格式一般是这样的:
- <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具体内容如下
- <?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>
可以看到PreferenceScreen的布局文件中主要的是两个TextView分别显示title和summary
我们在每个app中可以按照这个样式重新定义PreferenceScreen的样式,比如我们可以定义一个custom_preference.xml文件内容和上面的差不多,只不过重新定义了其text的大小和颜色,在
- <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
当然可能还有其他的更好的方法,希望和大家多多交流