所謂自定義控件(或稱組件)也就是編寫自己的控件類型,而非Android中提供的標准的控件,如TextView,CheckBox等等.不過自定義的控件一般也都是從標准控件繼承來的,或者是多種控件組合,或者是對標准控件的屬性進行改變而得到的自己滿意的控件.
自定義控件可能會有很多種方法,這里只介紹我要介紹的方法.
在這種方法中,大概的步驟是這樣的
1.我們的自定義控件和其他的控件一樣,應該寫成一個類,而這個類的屬性是是有自己來決定的.
2.我們要在res/values目錄下建立一個attrs.xml的文件,並在此文件中增加對控件的屬性的定義.
3.使用AttributeSet來完成控件類的構造函數,並在構造函數中將自定義控件類中變量與attrs.xml中的屬性連接起來.
4.在自定義控件類中使用這些已經連接的屬性變量.
5.將自定義的控件類定義到布局用的xml文件中去.
6.在界面中生成此自定義控件類對象,並加以使用.
好了,按照上述的方法,我們來看看http://blog.csdn.net/Android_Tutor/archive/2010/04/20/5508615.aspx
博客中的實例代碼,按步驟加以解釋:
1 //--------------------------------------------------------------------------------- 2 3 1. 定義自己的控件類:--------------------------------------------代碼1. 4 5 package com.android.tutor; 6 import android.content.Context; 7 import android.content.res.TypedArray; 8 import android.graphics.Canvas; 9 import android.graphics.Color; 10 import android.graphics.Paint; 11 import android.graphics.Rect; 12 import android.graphics.Paint.Style; 13 import android.util.AttributeSet; 14 import android.view.View; 15 16 17 public class MyView extends View 18 { 19 private Paint mPaint; 20 private Context mContext; 21 private static final String mString = "Welcome to Mr Wei's blog"; 22 23 public MyView(Context context) 24 { 25 super(context); 26 mPaint = new Paint(); 27 } 28 29 30 public MyView(Context context,AttributeSet attrs) 31 { 32 super(context,attrs); 33 mPaint = new Paint(); 34 35 TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyView); 36 int textColor = a.getColor(R.styleable.MyView_textColor,0XFFFFFFFF); 37 float textSize = a.getDimension(R.styleable.MyView_textSize, 36); 38 39 mPaint.setTextSize(textSize); 40 mPaint.setColor(textColor); 41 42 a.recycle(); 43 } 44 45 46 @Override 47 protected void onDraw(Canvas canvas) 48 49 { 50 // TODO Auto-generated method stub 51 super.onDraw(canvas); 52 //設置填充 53 mPaint.setStyle(Style.FILL); 54 55 //畫一個矩形,前倆個是矩形左上角坐標,后面倆個是右下角坐標 56 canvas.drawRect(new Rect(10, 10, 100, 100), mPaint); 57 58 mPaint.setColor(Color.BLUE); 59 //繪制文字 60 canvas.drawText(mString, 10, 110, mPaint); 61 } 62 }
代碼1定義了一個自定義控件,名字為MyView,是從View類繼承而來,也就是說它本身就是一種View,只是在View基礎上加工而成了我們自己的自定義控件MyView.在此類種黃色的兩行變量是我們新的屬性變量.
//---------------------------------------------------------------------------------
2. 在res/values目錄下建立一個attrs.xml的文件,並在此文件中增加對控件的屬性的定義--代碼2:
1 <?xml version="1.0" encoding="utf-8"?> 2 <resources> 3 <declare-styleable name="MyView"> 4 <attr name="textColor" format="color" /> 5 <attr name="textSize" format="dimension" /> 6 </declare-styleable> 7 </resources>
在<resources>標簽下使用<declare-styleable name="MyView">標簽來告訴框架它包含的屬性就是自定義控件MyView中的屬性.黃色的兩其實就對應了代碼1中黃色的變量.
//---------------------------------------------------------------------------------
3.使用AttributeSet來完成控件類的構造函數,並在構造函數中將自定義控件類中變量與attrs.xml中的屬性連接起來.
我們再看一下代碼1中的藍色代碼,其中使用AttributeSet來重載構造函數.在此函數中將類中的屬性變量與代碼二中定義的屬性聯系起來.
//---------------------------------------------------------------------------------
4.在自定義控件類中使用這些已經連接的屬性變量.
我們看一下代碼1中的黃色部分,就是對我們新定義的屬性的使用.
//---------------------------------------------------------------------------------
5.將自定義的控件類定義到布局用的xml文件中去.-----代碼3:
我們再看看布局的xml文件代碼:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" > 6 <TextView android:layout_width="fill_parent" 7 android:layout_height="wrap_content" 8 android:text="@string/hello" /> 9 10 <com.android.tutor.MyView android:layout_width="fill_parent" 11 android:layout_height="fill_parent" test:textSize="20px" test:textColor="#fff" /> 12 </LinearLayout>
其中紅色部分在布局中引用了我們MyView控件.
//---------------------------------------------------------------------------------
6.在界面中生成此自定義控件類對象,並加以使用.--------代碼4.
//---------------------------------------------------------------------------------