有时候系统提供给我们的preference并不能满足我们的要求,所以有的时候需要我们自定义Preferece,下面的例子就是我个人自定义的一个简单的带图标的Preference。
首先是xml布局文件,就是你想实现的布局。
<?xml version="1.0" encoding="utf-8"?>
<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">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="18dip"
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>
<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" />
</LinearLayout>
下面是自定义的preference的java文件:
import com.android.mms.R;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
public class IconListPreference extends Preference{
private Drawable mIcon;
public IconListPreference(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs);
this.setLayoutResource(R.layout.icon_list_preference);
//这里设置的是icon初始化的图标
this.mIcon = context.getResources().getDrawable(R.drawable.ycz20_black);
}
public IconListPreference(final Context context, final AttributeSet attrs) {
this(context, attrs, 0);
}
@Override
protected void onBindView(final View view) {
super.onBindView(view);
final ImageView imageView = (ImageView)view.findViewById(R.id.icon);
if ((imageView != null) && (this.mIcon != null)) {
imageView.setImageDrawable(this.mIcon);
}
}
/**
* Sets the icon for this Preference with a Drawable.
*
* @param icon The icon for this Preference
*/
public void setIcon(final Drawable icon) {
if (((icon == null) && (this.mIcon != null)) || ((icon != null) && (!icon.equals(this.mIcon)))) {
this.mIcon = icon;
this.notifyChanged();
}
}
public void setIcon(int iconRes) {
if(R.drawable.ycz20_black!=iconRes){
this.mIcon = getContext().getResources().getDrawable(iconRes);
this.notifyChanged();
}
}
/**
* Returns the icon of this Preference.
*
* @return The icon.
* @see #setIcon(Drawable)
*/
public Drawable getIcon() {
return this.mIcon;
}
}
比如你想更改ImageView里面的图标,就是以使用setIcon()方法。
因为自己完成的功能比较简单,所以重写的方法就比较少,大家 可以根据自己的需要来添加更多的方法。
而只是实现了一个Preferecnce,但是如何实现一个类似ListPreference类似的功能,但是比ListPreference更绚丽的效果呢?请看下一篇!