您的位置:首页 > Web前端

自定义Preference风格 | 扩展已有视图 | 自定义属性 | add icons to Preference | customize

2011-06-02 08:35 337 查看
一、效果

我们在Android原生系统中所看到的[无线和网络设置]界面,使用的是与Preference相关的视图完成的。使用Preference相关的视图能较好地进行了文字的分类和排版。如果我们希望进行图片文字混合显示的效果,就需要扩展Preference相关的视图




 

二、扩展PreferenceScreen

1.编写类IconPreferenceScreen派生Preference


   重写onBindView
方法,在该方法中通过获取某项属性来定义视图的显示。此处就是获取icon,并绘制在视图中。

IconPreferenceScreen.java

package com.coomix;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.preference.Preference;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
public class IconPreferenceScreen extends Preference {

private static final String TAG = "IconPreferenceScreen";
private Drawable mIcon;
public IconPreferenceScreen(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public IconPreferenceScreen(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setLayoutResource(R.layout.preference_icon);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.IconPreferenceScreen, defStyle, 0);
mIcon = a.getDrawable(R.styleable.IconPreferenceScreen_icon);
}
@Override
public void onBindView(View view) {
super.onBindView(view);
Log.i(TAG, "[onBindView](0)");
ImageView imageView = (ImageView) view.findViewById(R.id.icon);
if (imageView != null && mIcon != null) {
imageView.setImageDrawable(mIcon);
Log.i(TAG, "[onBindView](1)");
}
Log.i(TAG, "[onBindView](2)");
}
}


 

2.IconPreferenceScreen视图是具有显示图标属性,所以要用declare-styleable标签来进行自定义的属性
声明。

arrays.xml

<declare-styleable name="IconPreferenceScreen">
<attr name="icon" format="reference" />
</declare-styleable>


3.布局文件preference_icon.xml,该布局文件定义了ImageView,用作图标的显示。

<?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:id="@+android:id/widget_frame"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:paddingRight="?android:attr/scrollbarSize">

<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6dip"
android:layout_marginRight="6dip"
android:layout_gravity="center" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dip"
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="2" />
</RelativeLayout>
</LinearLayout>


 

4. 界面布局文件main.xml

注意自定义视图的声明方法,视图需要包名前缀。(com.coomix.IconPreferenceScreen)

自定义的属性也要指明名称空间。(coomix:icon)

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:coomix="http://schemas.android.com/apk/res/com.coomix">
<!-- 拍照、微博 -->
<PreferenceCategory android:title="@string/category_Fun">
<com.coomix.IconPreferenceScreen
android:key="toggle_cool_camera"
android:title="@string/Take_pic_upload_title"
android:summary="@string/Take_pic_upload_summary"
coomix:icon="@drawable/cool_camera">
<!-- <intent/> -->
</com.coomix.IconPreferenceScreen>
<com.coomix.IconPreferenceScreen
android:key="toggle_goome_weibo"
android:title="@string/link_weibo_title"
android:summary="@string/link_weibo_summary"
coomix:icon="@drawable/weibo">
<!-- <intent/> -->
</com.coomix.IconPreferenceScreen>
</PreferenceCategory>


三、扩展DialogPreference
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息