zoukankan      html  css  js  c++  java
  • android 定制PreferenceScreen

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

    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>  

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

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

    这里主要以PreferenceScreen为例说明:

    首先PreferenceScreen的布局文件在 framework中

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

    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>  

    可以看到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.            <!-- Arbitrary key/value pairs can be included for fragment arguments -->  
    6.            <extra android: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

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

  • 相关阅读:
    第46课.继承中的构造与析构
    第45课.不同的继承方式
    第44课.继承中的访问级别
    第43课.继承的概念和意义
    [文件系统]文件系统学习笔记(三)---目录项缓存dentry
    [文件系统]文件系统学习笔记(二)---task_struct
    [文件系统]文件系统学习笔记(一)---基本概念以及inode
    [Linux]进程(十二)--task_struct结构体
    [Linux]进程——用户态编程相关
    [linux]进程(十一)——进程权能
  • 原文地址:https://www.cnblogs.com/jackrex/p/3001280.html
Copyright © 2011-2022 走看看