View 视图树结构
Activity --> PhoneWindow --> DecorView
重要类:
LayoutParams : 布局参数,子View通过LayoutParams告诉父容器(ViewGroup)应该如何放置自己。
MeasureSpec:父容器对 view 的布局上的限制;
View ViewGroup 关系图:
自定义ViewGroup 关键是要重写 onMeasure onLayout 方法:
onMeasure 测量过程一般先测量子View,再测量自己;
测量子View:
测量子View需要的WidthMeasureSpec:widthMeasureSpec 、paddingLeft + paddingRight 自己需要的尺寸;width 子view尺寸、
int childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec, paddingLeft + paddingRight , width);
int childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec, paddingTop + paddingBottom , height);
测量子View:
childView.measure(childWidthMeasureSpec, childHeightMeasureSpec);
测量阶段获取View 尺寸:
int childHeight = childView.getMeasuredHeight();
int childWidth = childView.getMeasuredWidth();
测量结束后,按照需要布局保存测量结果;
测量自己:
根据测量模式,将自己的尺寸设置
int realWidth = (widthMode == MeasureSpec.EXACTLY) ? widthSize: parentNeededWidth;
int realHeight = (heightMode == MeasureSpec.EXACTLY) ? heightSize: parentNeededHeight;
setMeasuredDimension(realWidth, realHeight);
布局view onLayout,将View 布局在界面,遍历所有View,将获取的坐标设置,完成布局
view.layout(left,top,right,bottom);
补充:MeasureMode 对,测量结果的影响,见
https://www.jianshu.com/p/2fffa71ac5ba
https://blog.csdn.net/qq_40881680/article/details/82378452 https://blog.csdn.net/sinat_29874521/article/details/79994169



