有時(shí),Android系統(tǒng)控件無(wú)法滿足我們的需求,因此有必要自定義View。具體方法參見(jiàn)官方開(kāi)發(fā)文檔:http://developer.android.com/guide/topics/ui/custom-components.html
一般來(lái)說(shuō),自定義控件都會(huì)去重寫(xiě)View的onMeasure方法,因?yàn)樵摲椒ㄖ付ㄔ摽丶谄聊簧系拇笮 ?/p>
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)
onMeasure傳入的兩個(gè)參數(shù)是由上一層控件傳入的大小,有多種情況,重寫(xiě)該方法時(shí)需要對(duì)計(jì)算控件的實(shí)際大小,然后調(diào)用setMeasuredDimension(int, int)設(shè)置實(shí)際大小。
onMeasure傳入的widthMeasureSpec和heightMeasureSpec不是一般的尺寸數(shù)值,而是將模式和尺寸組合在一起的數(shù)值。我們需要通過(guò)int mode = MeasureSpec.getMode(widthMeasureSpec)得到模式,用int size = MeasureSpec.getSize(widthMeasureSpec)得到尺寸。
mode共有三種情況,取值分別為MeasureSpec.UNSPECIFIED, MeasureSpec.EXACTLY, MeasureSpec.AT_MOST。
MeasureSpec.EXACTLY是精確尺寸,當(dāng)我們將控件的layout_width或layout_height指定為具體數(shù)值時(shí)如andorid:layout_width="50dip",或者為FILL_PARENT是,都是控件大小已經(jīng)確定的情況,都是精確尺寸。
MeasureSpec.AT_MOST是最大尺寸,當(dāng)控件的layout_width或layout_height指定為WRAP_CONTENT時(shí),控件大小一般隨著控件的子空間或內(nèi)容進(jìn)行變化,此時(shí)控件尺寸只要不超過(guò)父控件允許的最大尺寸即可。因此,此時(shí)的mode是AT_MOST,size給出了父控件允許的最大尺寸。
MeasureSpec.UNSPECIFIED是未指定尺寸,這種情況不多,一般都是父控件是AdapterView,通過(guò)measure方法傳入的模式。
因此,在重寫(xiě)onMeasure方法時(shí)要根據(jù)模式不同進(jìn)行尺寸計(jì)算。下面代碼就是一種比較典型的方式:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(getMeasuredLength(widthMeasureSpec, true), getMeasuredLength(heightMeasureSpec, false));
}
private int getMeasuredLength(int length, boolean isWidth) {
int specMode = MeasureSpec.getMode(length);
int specSize = MeasureSpec.getSize(length);
int size;
int padding = isWidth ? getPaddingLeft() + getPaddingRight()
: getPaddingTop() + getPaddingBottom();
if (specMode == MeasureSpec.EXACTLY) {
size = specSize;
} else {
size = isWidth ? padding + mWave.length / 4 : DEFAULT_HEIGHT
+ padding;
if (specMode == MeasureSpec.AT_MOST) {
size = Math.min(size, specSize);
}
}
return size;
}