IT开发者百科  > 所属分类  >  Preference   
[0] 评论[0] 编辑

android 定制PreferenceScreen样式

在使用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
 
当然可能还有其他的更好的方法,希望和大家多多交流

附件列表


0

Java-Android手机千人开发交流QQ群:38088312,PHP开发千人高级交流QQ群:50194090,欢迎加入学习!本站为
非赢利站点,挖掘网络资源,分享个人兴趣,如有侵犯您的版权,请联系我们,我们会第一时间删除内容或添加转载出处,敬请谅解!

如果您认为本词条还有待完善,请 编辑

上一篇 Android SDK工具:使用layoutopt进行布局优化    下一篇 上篇 下篇 如何修改IIS上传大小限制

标签

同义词

暂无同义词