此方法适合APP有太多的图片做成的按钮,不想写大量drawable的xml文件,可以使用此种方法,直接3个属性搞定,当然,你也可以通过拓展,以达到跟drawable一样,可以管理fouce、enable等状态

1、创建一个YybButton类继承Button

public class YybButton extends Button implements OnTouchListener{

	private Context context;
	private int defaultImage;
	private int clickImage;
	private int selectImage;

	public YybButton(Context context) {
		super(context);
	}

	public YybButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		init(context, attrs);
	}

	public YybButton(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context, attrs);
	}

	private void init(Context context, AttributeSet attrs) {
		this.context = context;
		this.setOnTouchListener(this);
		/* 使用res/values/attrs.xml中的<declare-styleable>定义的Gallery属性. */
		TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.YybButon);
		
		/* 取得com.yyb.ui.YybButon属性的Index id */
		defaultImage = a.getResourceId(R.styleable.YybButon_defaultImage, 0);
		clickImage = a.getResourceId(R.styleable.YybButon_clickImage, 0);
		selectImage = a.getResourceId(R.styleable.YybButon_selectImage, 0);
		/* 让对象的styleable属性能够反复使用 */
		a.recycle();
	}

	public boolean onTouch(View v, MotionEvent event) {

		if(event.getAction() == MotionEvent.ACTION_DOWN){
			if(clickImage != 0){
				this.setBackgroundResource(clickImage);
			}else{
				this.setBackgroundColor(0xff777777);
			}
			return true;
		}else if(event.getAction() == MotionEvent.ACTION_UP){
			if(defaultImage != 0){
				this.setBackgroundResource(defaultImage);
			}else{
				this.setBackgroundColor(0xff333333);
			}
			return true;
		}
		
		return false;
	}
	

	@Override
	public void setSelected(boolean selected) {
		super.setSelected(selected);
		if(selected){
			if(selectImage != 0){
				this.setBackgroundResource(selectImage);
			}
		}else{
			if(defaultImage != 0){
				this.setBackgroundResource(defaultImage);
			}
		}
	}
}

这里要注意几点:1)、要实现Button的构造方法,

public YybButton(Context context, AttributeSet attrs) {
public YybButton(Context context, AttributeSet attrs, int defStyle) {

因为如果自定义的View在XML布局文件中使用的话,默认会调用这两者中的一个,主要是前一个


2)、实现OnTouchListener接口,并对Button的Touch事件进行处理,主要是默认状态和按下状态的图片,后来考虑到拓展,增加了一个选中状态,这里重写了setSelect()这个方法,并对选中状态也做了图片的处理


2、在res/value文件夹中创建一个attrs的文件,并写入
<?xml version="1.0" encoding="utf-8"?>
<resources>
  <declare-styleable name="YybButon">
      <attr name="defaultImage" format="reference"></attr>
      <attr name="clickImage" format="reference"></attr>
      <attr name="selectImage" format="reference"></attr>
  </declare-styleable>
</resources>

主要是声明了一个YybButton的属性列表,分别对应了Button的默认状态、按钮状态、选中状态,reference主要是声明了这个属性的类型为引用,即使用drawable图片attrs中所有format值的格式http://blog.csdn.net/yyb346275563/article/details/8521108

好了,下面大家可以试试,自己自定义的Button了

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:yyb="http://schemas.android.com/apk/res/yyb.yu.babystudy"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/poetry_bg"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/poetry_title" >
    <com.yyb.ui.YybButton
        android:id="@+id/exit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/exit"
       yyb:clickImage="@drawable/exit_c"
      yyb:defaultImage="@drawable/exit"/>
    </LinearLayout>

</LinearLayout>

这里的yyb必须在layout文件的开头声明,yyb可以随便自定义,yyb.yu.babystudy为你的应用当前的包名,OK,搞定

posted on 2013-01-19 15:29  Super小宝  阅读(1502)  评论(0编辑  收藏  举报