ViewGroup是View的容器類计技,里面會(huì)包含多個(gè)View。經(jīng)常用的LinearLayout,RelativeLayout等都是ViewGroup的子類。
還是從方法開始說明ViewGroup捷犹,Android 自定義View(二)函數(shù)分析 中已經(jīng)有說明了一下方法函數(shù)的意思,ViewGroup的實(shí)現(xiàn)方法有必要的兩個(gè) onMeasure 和 onLayout 和自定義View的不同的是:
onDraw在自定義ViewGroup是冕末,一般是調(diào)用了子類的onDraw方法萍歉,ViewGroup是View的容器,本身一般不需要draw額外的修飾档桃,所以往往在onDraw方法里面枪孩,只需要調(diào)用ViewGroup的onDraw默認(rèn)實(shí)現(xiàn)方法即可。(自定義View時(shí)onLayout是空方法,ViewGroup是onLayout卻是必須實(shí)現(xiàn)的)
onMeasure
Measure過程還是測量ViewGroup的大小蔑舞,如果layout_widht和layout_height是match_parent或具體的dp大小拒担,直接調(diào)用setMeasuredDimension()方法,設(shè)置ViewGroup的寬高即可攻询,如果是wrap_content从撼,我們需要遍歷所有的子View,然后對每個(gè)子View進(jìn)行測量钧栖,然后根據(jù)子View的排列規(guī)則低零,計(jì)算出最終ViewGroup的大小。onLayout
layout過程其實(shí)就是對子View的位置進(jìn)行排列拯杠,onLayout方法給我一個(gè)機(jī)會(huì)掏婶,來按照我們想要的規(guī)則自定義子View排列。
一個(gè)簡單的栗子(ViewGroup 中的 View 排成一列)看下 onMeasure 和 onLayout 的代碼實(shí)現(xiàn):
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec,heightMeasureSpec);
measureChildren(widthMeasureSpec,heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int height = 0;
View child;
for(int i = 0,size = getChildCount();i < size;i++) {
child = getChildAt(i);
child.layout(0, height, child.getMeasuredWidth(),height + child.getMeasuredHeight());
height += child.getMeasuredHeight();
}
}
要實(shí)現(xiàn)View一列顯示潭陪,然后每個(gè)子View的寬度是一樣的雄妥,并且每個(gè)子View的left和right是一樣的。所以每個(gè)子View只有top和bottom不一樣依溯。我們首先定義個(gè)高度height初始為0老厌,然后得到所有子View的個(gè)數(shù),依次設(shè)置每個(gè)子View的top和bottom誓沸。top就是定義的height,bottom則為height加上子View的高度梅桩。設(shè)置完后height累加。
LayoutParams
LayoutParams存儲(chǔ)了子View在加入ViewGroup中時(shí)的一些參數(shù)信息拜隧,在繼承ViewGroup類時(shí)宿百,一般也需要新建一個(gè)新的LayoutParams類,就像SDK中我們熟悉的LinearLayout.LayoutParams洪添,RelativeLayout.LayoutParams類等一樣垦页,那么可以這樣做,在你定義的ViewGroup子類中干奢,新建一個(gè)LayoutParams類繼承與ViewGroup.LayoutParams痊焊。實(shí)際使用:
@Override
protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
int childCount = this.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = this.getChildAt(i);
LayoutParams lParams = (LayoutParams) child.getLayoutParams();
child.layout(lParams.left, lParams.top, lParams.left + childWidth,
lParams.top + childHeight);
}
}
實(shí)例應(yīng)用
自定義一個(gè)根據(jù)屏幕寬度自動(dòng)換行的ViewGroup:
public class LineBreakLayout extends ViewGroup implements View.OnClickListener {
private final static int VIEW_MARGIN = 2;
private int widthMargin = VIEW_MARGIN;//view width space
private int heightMargin = VIEW_MARGIN;//view height space
private LineLayoutItemListener mListener;
public LineBreakLayout(Context context) {
super(context);
}
public LineBreakLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public LineBreakLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setViewMargin(int widthMargin, int heightMargin) {
this.widthMargin = widthMargin;
this.heightMargin = heightMargin;
}
public void setOnLineLayoutItemListener(LineLayoutItemListener listener) {
mListener = listener;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//得到ViewGroup的初始寬高
final int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec) + getPaddingBottom()+getPaddingTop();
int line_height = 0;
//獲取第一個(gè)子View的起始點(diǎn)位置
int xpos = getPaddingLeft();
int ypos = getPaddingTop();
//計(jì)算每一個(gè)子View的尺寸,并算出ViewGroup的高度
for (int index = 0; index < getChildCount(); index++) {
final View child = getChildAt(index);
if (child.getVisibility() == GONE) {
continue;
}
child.setId(index);
child.setOnClickListener(this);
final LayoutParams lp = child.getLayoutParams();
//算出子View寬的MeasureSpec值
int wSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.UNSPECIFIED);
//算出子View高的MeasureSpec值
int hSpec = MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.UNSPECIFIED);
//讓子View記住自己寬高的MeasureSpec值,子View的
//函數(shù)傳入的就是這里算出來的這兩個(gè)值
child.measure(wSpec, hSpec);
//設(shè)置完MeasureSpec值后調(diào)用View.getMeasuredWidth()函數(shù)算出View的寬度
final int childw = child.getMeasuredWidth();
//記錄最大行高(子View的高度有可能不一樣,行高取最大高度)
line_height = Math.max(line_height, child.getMeasuredHeight() + heightMargin);
if (xpos + childw + widthMargin > width) {
//初始坐標(biāo)的x偏移值+子View寬度>ViewGroup寬度 就換行
xpos = getPaddingLeft();//坐標(biāo)x偏移值歸零
ypos += line_height; //坐標(biāo)y偏移值再加上本行的行高也就是換行
}
//算出下一個(gè)子View的起始點(diǎn)x偏移值
xpos += childw + widthMargin;
}
if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.UNSPECIFIED) {
//對高度期望值沒有限制
height = ypos + line_height + heightMargin;
} else if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST) {
//達(dá)不到指定高度則縮小高度
if (ypos + line_height < height) {
height = ypos + line_height + heightMargin;
}
} else {
height = ypos + line_height + heightMargin;
}
//設(shè)置ViewGroup寬高值
setMeasuredDimension(width, height);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
final int count = getChildCount();
int parentWidth = right - left;
top = 0;//clear top distance
int row = 0;// which row lay you view relative to parent
int lengthX = 0; // right position of child relative to parent
int lengthY = top; // bottom position of child relative to parent
//計(jì)算每一個(gè)子View的尺寸,并算出ViewGroup的高度
for (int i = 0; i < count; i++) {
final View child = this.getChildAt(i);
int width = child.getMeasuredWidth();
int height = child.getMeasuredHeight();
lengthX += width + widthMargin;
lengthY = row * (height + heightMargin) + heightMargin + height + top;
// if it can't drawing on a same line , skip to next line
if (lengthX > parentWidth) {
lengthX = width + widthMargin;
row ++;
lengthY = row * (height + heightMargin) + heightMargin + height + top;
}
child.layout(lengthX - width, lengthY - height, lengthX, lengthY);
}
}
@Override
public void onClick(View v) {
if (mListener != null) {
int position = v.getId();
mListener.onLineItemClick(this, v, position);
}
}
public interface LineLayoutItemListener {
void onLineItemClick(ViewGroup parent, View view, int position);
}
}