android 定制PreferenceScreen

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

  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>
    <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具体内容如下

  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>
<?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的大小和颜色,在

  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>
 <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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值