/** * {@inheritDoc} */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int count = getChildCount(); final boolean measureMatchParentChildren = MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.EXACTLY || MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.EXACTLY; //当FrameLayout的宽和高,只有同时设置为match_parent或者指定的size,那么这个 //measureMatchParentChlidren = false,否则为true。下面会用到这个变量 mMatchParentChildren.clear(); int maxHeight = 0; int maxWidth = 0; int childState = 0; //宽高的期望类型 for (int i = 0; i < count; i++) { //一次遍历每一个不为GONE的子view final View child = getChildAt(i); if (mMeasureAllChildren || child.getVisibility() != GONE) { //去掉FrameLayout的左右padding,子view的左右margin,这时候,再去 //计算子view的期望的值 measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0); final LayoutParams lp = (LayoutParams) child.getLayoutParams(); /*maxWidth找到子View中最大的宽,高同理,为什么要找到他,因为在这里,FrameLayout是wrap -content.他的宽高肯定受子view的影响*/ 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()); /*下面的判断,只有上面的FragLayout的width和height都设置为match_parent 才不会执行 此处的mMatchParentChlidren的list里存的是设置为match_parent的子view。 结合上面两句话的意思,当FrameLayout设置为wrap_content,这时候要把所有宽高设置为 match_parent的子View都记录下来,记录下来干什么呢? 这时候FrameLayout的宽高同时受子View的影响*/ if (measureMatchParentChildren) { if (lp.width == LayoutParams.MATCH_PARENT || lp.height == LayoutParams.MATCH_PARENT) { mMatchParentChildren.add(child); } } } } // Account for padding too maxWidth += getPaddingLeftWithForeground() + getPaddingRightWithForeground(); maxHeight += getPaddingTopWithForeground() + getPaddingBottomWithForeground(); // Check against our minimum height and width maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight()); maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth()); // Check against our foreground‘s minimum height and width final Drawable drawable = getForeground(); if (drawable != null) { maxHeight = Math.max(maxHeight, drawable.getMinimumHeight()); maxWidth = Math.max(maxWidth, drawable.getMinimumWidth()); } //设置测量过的宽高 setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, childState), resolveSizeAndState(maxHeight, heightMeasureSpec, childState << MEASURED_HEIGHT_STATE_SHIFT)); count = mMatchParentChildren.size();//这个大小就是子view中设定为match_parent的个数 if (count > 1) { for (int i = 0; i < count; i++) { //这里看上去重新计算了一遍 final View child = mMatchParentChildren.get(i); final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams(); int childWidthMeasureSpec; int childHeightMeasureSpec; /*如果子view的宽是match_parent,则宽度期望值是总宽度-padding-margin 如果子view的宽是指定的比如100dp,则宽度期望值是padding+margin+width 这个很容易理解,下面的高同理*/ if (lp.width == LayoutParams.MATCH_PARENT) { childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth() - getPaddingLeftWithForeground() - getPaddingRightWithForeground() - lp.leftMargin - lp.rightMargin, MeasureSpec.EXACTLY); } else { childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec, getPaddingLeftWithForeground() + getPaddingRightWithForeground() + lp.leftMargin + lp.rightMargin, lp.width); } if (lp.height == LayoutParams.MATCH_PARENT) { childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight() - getPaddingTopWithForeground() - getPaddingBottomWithForeground() - lp.topMargin - lp.bottomMargin, MeasureSpec.EXACTLY); } else { childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec, getPaddingTopWithForeground() + getPaddingBottomWithForeground() + lp.topMargin + lp.bottomMargin, lp.height); } //把这部分子view重新计算大小 child.measure(childWidthMeasureSpec, childHeightMeasureSpec); } } }
时间: 2024-10-14 01:26:57