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);}//对孩子进行摆放@Overrideprotected 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();}}
本文详细介绍了Android中ViewGroup的绘制流程,包括onMeasure和onLayout方法的作用及实现方式,并通过一个交叉布局的实例展示了如何自定义ViewGroup来实现特定的布局效果。

被折叠的 条评论
为什么被折叠?



