Android 自定義 Preference

有些時候系統提供的 Preference 不滿足我們的要求的時候,我們就需要自己定製了。現在產品要求 ChekBoxPreference? 的 summary 的顏色要能動態改變,在關閉的時候是默認顏色,在開啓的時候變成紅色。現在我們就可以自己定製啦。

簡單的修改 xml

先說說簡單的情況。如果字體顏色只是靜態的話,可以不用改代碼,改改 layout xml 就好了。系統自己的 xml 是 framework/base/core/res/res/layout/preference.xml 。把這個文件複製,然後在其基礎上改改 text 的屬性就好了,然後在使用 CheckBoxPreference? 的時候指定自己定製的 xml(這裏我叫 custom_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" />

// 就是在這裏改啦,加一個 textColor 屬性
<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:textColor="#FFFF0000" 
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>

// 改好後,在用 CheckBoxPreference 的時候,指定自己的 xml layout
<CheckBoxPreference android:key="lock_screen" 
android:title="@string/settings_lock_screen_title" 
android:defaultValue="false" 
android:layout="@layout/custom_preference"
android:summaryOff="@string/settings_lock_screen_off_summary" 
android:summaryOn="@string/settings_lock_screen_on_summary" 
android:summary="@string/settings_lock_screen_off_summary">
</CustomCheckBoxPreference>

擴展代碼

如果需要複雜點的功能,就需要繼承 CheckBoxPreference? ,然後自己定製代碼了。這裏以動態的改字體顏色爲例:繼承 CheckBoxPreference? 後,重載父類的 onBindView 函數(這個函數好像是在將數據顯示到視圖上的時候調用的):

public class CustomCheckBoxPreference extends CheckBoxPreference {
        
        public CustomCheckBoxPreference(Context context, AttributeSet attrs,
        int defStyle) {
                super(context, attrs, defStyle);
                // TODO Auto-generated constructor stub
        }
        
        @Override
        protected void onBindView(View view) {
                super.onBindView(view);
                
                boolean isChecked = isChecked();
                Resources res = view.getResources();
                
                TextView summaryView = (TextView)view.findViewById(com.android.internal.R.id.summary);
                if (summaryView != null) {
                        if (isChecked) {
                                summaryView.setTextColor(res.getColor(R.color.red));
                        } else {
                                //summaryView.setTextAppearance(getContext(), com.android.internal.R.attr.textAppearanceSmall);
                                summaryView.setTextColor(res.getColor(com.android.internal.R.color.secondary_text_light));
                        }
                }
        }
}

寫好自己的類之後,在用的時候把原來的 CheckBoxPreference? 換成自己的類就可以了:

<cn.fmsoft.launcher2.CustomCheckBoxPreference android:key="lock_screen" 
android:title="@string/settings_lock_screen_title" 
android:defaultValue="false" 
android:summaryOff="@string/settings_lock_screen_off_summary" 
android:summaryOn="@string/settings_lock_screen_on_summary" 
android:summary="@string/settings_lock_screen_off_summary">
</cn.fmsoft.launcher2.CustomCheckBoxPreference>

參考

參考此處:Preference 使用小結
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章