在使用PreferenceActivity的时候,布局文件的格式一般是这样的:

[html] view plain copy print ?
  1. <PreferenceCategory
  2. android:title="@string/launch_preferences">
  3. <!--ThisPreferenceScreentagsendstheusertoanewfragmentof
  4. preferences.Ifrunninginalargescreen,theycanbeembedded
  5. insideoftheoverallpreferencesUI.-->
  6. <PreferenceScreen
  7. android:fragment="com.example.android.apis.preference.PreferenceWithHeaders$Prefs1FragmentInner"
  8. android:title="@string/title_fragment_preference"
  9. android:summary="@string/summary_fragment_preference">
  10. <!--Arbitrarykey/valuepairscanbeincludedforfragmentarguments-->
  11. <extraandroid:name="someKey"android:value="somePrefValue"/>
  12. </PreferenceScreen>
  13. <!--ThisPreferenceScreentagsendstheusertoacompletelydifferent
  14. activity,switchingoutofthecurrentpreferencesUI.-->
  15. <PreferenceScreen
  16. android:title="@string/title_intent_preference"
  17. android:summary="@string/summary_intent_preference">
  18. <intentandroid:action="android.intent.action.VIEW"
  19. android:data="http://www.android.com"/>
  20. </PreferenceScreen>
  21. </PreferenceCategory>

但是我们不能控制其中的title和summary的字体的样式,使用的是系统的样式

怎么样修改title和summary的字体和颜色呢?

这里主要以PreferenceScreen为例说明:

首先PreferenceScreen的布局文件在 framework中

位置如下 /framework/base/core/res/res/layout/preference.xml具体内容如下

[html] view plain copy print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <!--Copyright(C)2006TheAndroidOpenSourceProject
  3. LicensedundertheApacheLicense,Version2.0(the"License");
  4. youmaynotusethisfileexceptincompliancewiththeLicense.
  5. YoumayobtainacopyoftheLicenseat
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unlessrequiredbyapplicablelaworagreedtoinwriting,software
  8. distributedundertheLicenseisdistributedonan"ASIS"BASIS,
  9. WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied.
  10. SeetheLicenseforthespecificlanguagegoverningpermissionsand
  11. limitationsundertheLicense.
  12. -->
  13. <!--LayoutforaPreferenceinaPreferenceActivity.The
  14. Preferenceisabletoplaceaspecificwidgetforitsparticular
  15. typeinthe"widget_frame"layout.-->
  16. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:minHeight="?android:attr/listPreferredItemHeight"
  20. android:gravity="center_vertical"
  21. android:paddingRight="?android:attr/scrollbarSize">
  22. <RelativeLayout
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:layout_marginLeft="15dip"
  26. android:layout_marginRight="6dip"
  27. android:layout_marginTop="6dip"
  28. android:layout_marginBottom="6dip"
  29. android:layout_weight="1">
  30. <TextViewandroid:id="@+android:id/title"
  31. android:layout_width="wrap_content"
  32. android:layout_height="wrap_content"
  33. android:singleLine="true"
  34. android:textAppearance="?android:attr/textAppearanceLarge"
  35. android:ellipsize="marquee"
  36. android:fadingEdge="horizontal"/>
  37. <TextViewandroid:id="@+android:id/summary"
  38. android:layout_width="wrap_content"
  39. android:layout_height="wrap_content"
  40. android:layout_below="@android:id/title"
  41. android:layout_alignLeft="@android:id/title"
  42. android:textAppearance="?android:attr/textAppearanceSmall"
  43. android:maxLines="4"/>
  44. </RelativeLayout>
  45. <!--Preferenceshouldplaceitsactualpreferencewidgethere.-->
  46. <LinearLayoutandroid:id="@+android:id/widget_frame"
  47. android:layout_width="wrap_content"
  48. android:layout_height="match_parent"
  49. android:gravity="center_vertical"
  50. android:orientation="vertical"/>
  51. </LinearLayout>

可以看到PreferenceScreen的布局文件中主要的是两个TextView分别显示title和summary

我们在每个app中可以按照这个样式重新定义PreferenceScreen的样式,比如我们可以定义一个custom_preference.xml文件内容和上面的差不多,只不过重新定义了其text的大小和颜色,在

[html] view plain copy print ?
  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. <!--Arbitrarykey/valuepairscanbeincludedforfragmentarguments-->
  6. <extraandroid:name="someKey"android:value="somePrefValue"/>
  7. </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

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

更多相关文章

  1. android中自定义Button,设置不同背景图片。
  2. android mtk log宏定义
  3. [android]寫一個system/bin 下的可執行文件
  4. 分享一个圆角自定义的漂亮AlertDialog
  5. Android mediaScanner 删除U盘上的文件
  6. Android--自定义Dialog,仿IOS对话框样式
  7. android tabHost布局之一 不继承TabActivity并以布局文件进行布

随机推荐

  1. Android(安卓)布局阴影实现
  2. Android怎么办? iPad 2降价100美元继续出
  3. Android(安卓)Studio下运行开源项目9GAG
  4. 处女男学Android(三)---Handler简介以及初
  5. Android图片内存优化-Android(安卓)8.0上
  6. 实时改变配置
  7. 为Android应用程序读取/dev下设备而提权(
  8. Android多渠道打包相关介绍
  9. Android手机软件汉化教程---第五课 打包
  10. android 2.0发布