1.viewGroup绘制流程
总结:当继承ViewGroup的时候必须要重写onMeasure方法和onLayout方法,在onMeasure方法里面完成对孩子的测量,在onLayout方法里面完成对孩子的摆放.
当继承View的时候必须要重写onMeasure方法和onDraw方法 在onMeasure方法里面完成对当前view的测量,在onDraw完成绘制.
2.getMesasuredWidth和getwidth区别
getMesasuredWidth是对view测量后使用
getWidth 是对view排版后才可以使用
3.交叉布局
public class CrossLayout extends RelativeLayout {
private boolean isLeft;
public CrossLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
//对孩子进行摆放
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int top = 0;
//1.找到所有的孩子
for(int i=0;i<getChildCount();i++){
//2.找到所有的孩子
View child = getChildAt(i);
//3.对孩子进行摆放
int left = 0;
//3.1 对isleft 判断 如果为true 就摆到左边 否则就摆到右边
if (isLeft){
//4.判断是奇数还是偶数 如果是奇数就摆到左边 如果是偶数就摆到右边
if (i % 2 ==0){
//对于控件是奇数 就摆到左边
left = 0;
}else{
//对于控件来说是偶数 摆到右边 left = 容器宽度 - 孩子控件的宽度
left = getMeasuredWidth() - child.getMeasuredWidth();
}
}else{
//5.判断是奇数还是偶数 如果是奇数就摆到左边 如果是偶数就摆到右边
if (i % 2 ==0){
//对于控件是奇数 就摆到左边
left = getMeasuredWidth() - child.getMeasuredWidth();
}else{
//对于控件来说是偶数 摆到右边 left = 容器宽度 - 孩子控件的宽度
left = 0;
}
}
int right = left+child.getMeasuredWidth();
int bootom = top + child.getMeasuredHeight();
child.layout(left,top,right,bootom);
top += child.getMeasuredHeight();
}
}
//用来实现布局切换
public void startSwitchLayout() {
isLeft =!isLeft;
//请求重新排版
requestLayout();
}
}