/**
* {@inheritDoc}
* 如果,子 view如果设置属性是 match_parent,
* 如果,measureMatchParentChildren是true (其实就是framlayout模式是非精准模式)
* 那么这种子view是被测量了两次
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int count = getChildCount();
//当FrameLayout的宽和高 只有同时设置为 match_parent
//当FrameLayout的宽和高 指定的size
///measureMatchParentChlidren = false,否则为true。
//MeasureSpec.EXACTLY时,就表示当前视图的大小等于参数measureSpec中所指定的值。
//当FrameLayout 非精准模式,这时候要把所有宽高设置为 //match_parent的子View都记录下来
// 这时候FrameLayout的宽高同时受子View的影响
final boolean measureMatchParentChildren = MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.EXACTLY || MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.EXACTLY;
mMatchParentChildren.clear();
int maxHeight = 0;
int maxWidth = 0;
int childState = 0;//宽高的期望类型
//测量孩子同时,计算出一个最大宽高
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (mMeasureAllChildren || child.getVisibility() != GONE) { //一次遍历每一个不为GONE的子view
//第一次测量孩子
measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
maxWidth = Math.max(maxWidth, child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin);
maxHeight = Math.max(maxHeight, child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin);
childState = combineMeasuredStates(childState, child.getMeasuredState());
//的mMatchParentChlidren的list里存的是设置为match_parent的子view。
if (measureMatchParentChildren) {
if (lp.width == LayoutParams.MATCH_PARENT || lp.height == LayoutParams.MATCH_PARENT) {
mMatchParentChildren.add(child);
}
FrameLayout 测量过程(代码流水线)
最新推荐文章于 2022-03-04 18:32:04 发布