onMeasure先measure孩子再measure自身
measureChildren(widthMeasureSpec,heightMeasureSpec);
或者
int count = getChildCount();
int totalHeight = MeasureSpec.getSize(heightMeasureSpec);
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
measureChild(child, widthMeasureSpec,heightMeasureSpec);
}
measure自身
setMeasuredDimension(widthMeasureSpec, resolveSize(totalHeight, heightMeasureSpec));
onLayout布置孩子的显示
//对孩子进行布局的方法
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int width = getMeasuredWidth() / 2;
int count = getChildCount();
int currentHeight = 0;
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
int left = 0;
if (i % 2 != 0) {
left = width;
}
child.layout(left, currentHeight,
left + width, currentHeight + child.getMeasuredHeight());
//累计高度
currentHeight += child.getMeasuredHeight();
}
}
measureChildren(widthMeasureSpec,heightMeasureSpec);
或者
int count = getChildCount();
int totalHeight = MeasureSpec.getSize(heightMeasureSpec);
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
measureChild(child, widthMeasureSpec,heightMeasureSpec);
}
measure自身
setMeasuredDimension(widthMeasureSpec, resolveSize(totalHeight, heightMeasureSpec));
onLayout布置孩子的显示
//对孩子进行布局的方法
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int width = getMeasuredWidth() / 2;
int count = getChildCount();
int currentHeight = 0;
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
int left = 0;
if (i % 2 != 0) {
left = width;
}
child.layout(left, currentHeight,
left + width, currentHeight + child.getMeasuredHeight());
//累计高度
currentHeight += child.getMeasuredHeight();
}
}