自定义图文并茂的Button

anroid内置了Button和ImageButton,但是没有提供既能显示图片又能显示文字的button。 
这里我自定义了一个ImageTextButton 
其中了XML文件中使用了自定义属性custom:icon="@drawable/icon" 
下面是ImageTextButton源码: 

Java代码 
  1. import Android.content.Context;  
  2. import android.graphics.Bitmap;  
  3. import android.graphics.BitmapFactory;  
  4. import android.graphics.Canvas;  
  5. import android.util.AttributeSet;  
  6. import android.widget.Button;  
  7.   
  8. public class ImageTextButton extends Button {  
  9.     private final String namespace =  "http://www.javaeye.com/custom";  
  10.     private int resourceId =0;  
  11.     private Bitmap bitmap;  
  12.     public ImageTextButton(Context context, AttributeSet attrs) {  
  13.         super(context, attrs);  
  14.         // TODO Auto-generated constructor stub  
  15.         setClickable(true);  
  16.         //默认使用R.drawable.icon这张图片  
  17.         resourceId = attrs.getAttributeResourceValue(namespace, "icon", R.drawable.icon);  
  18.         bitmap = BitmapFactory.decodeResource(getResources(), resourceId);  
  19.     }  
  20.     @Override  
  21.     protected void onDraw(Canvas canvas) {  
  22.         // TODO Auto-generated method stub  
  23.         //图片顶部居中显示  
  24.         int x=(this.getMeasuredWidth()-bitmap.getWidth())>>1;  
  25.         int y=0;  
  26.         canvas.drawBitmap(bitmap, x, y, null);   
  27.         //坐标需要转换,因为默认情况下Button中的文字居中显示  
  28.         //这里需要让文字在底部显示  
  29.         canvas.translate(0, (this.getMeasuredHeight()>>1)-(int)this.getTextSize());  
  30.           
  31.         super.onDraw(canvas);  
  32.     }  
  33.   
  34.     public void setIcon(Bitmap bitmap){  
  35.         this.bitmap=bitmap;  
  36.                 invalidate();  
  37.     }  
  38.     public void setIcon(int resourceId){  
  39.         this.bitmap=BitmapFactory.decodeResource(getResources(), resourceId);  
  40.                 invalidate();  
  41.     }  
  42. }  


Xml代码 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:custom="http://www.javaeye.com/custom"  
  4.     android:orientation="vertical"  
  5.     android:layout_width="fill_parent"  
  6.     android:layout_height="fill_parent"  
  7.     >  
  8.   
  9.     <com.wt.app.ImageTextButton   
  10.     android:text="OK"  
  11.     custom:icon="@drawable/icon"  
  12.     android:id="@+id/button_0"   
  13.     android:layout_width="80dip"   
  14.     android:layout_height="80dip"  
  15.     />  
  16.     <com.wt.app.ImageTextButton   
  17.     android:text="OK"  
  18.     custom:icon="@drawable/icon"  
  19.     android:id="@+id/button_1"   
  20.     android:layout_width="80dip"   
  21.     android:layout_height="100dip"  
  22.     android:textSize="30dip"  
  23.     android:textColor="#ff0000"  
  24.     />  
  25.       
  26.     <com.wt.app.ImageTextButton   
  27.     android:text="OK"  
  28.     custom:icon="@drawable/icon"  
  29.     android:id="@+id/button_2"   
  30.     android:layout_width="200dip"   
  31.     android:layout_height="200dip"  
  32.     android:textSize="30dip"  
  33.     />  
  34.      
  35. </LinearLayout> 

导航