FrameLayout的测量

来源:互联网 发布:富途网络怎么样 编辑:程序博客网 时间:2024/04/29 11:33
    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        int count = getChildCount();    //如果传入的测量规格有一个不是match_parent,则将其标为true(这里为了处理当一个父控件,比如FrameLayout布局参数设置为wrap_content,而其子view有的设置为match_parent的时候出现的矛盾)        final boolean measureMatchParentChildren =                MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.EXACTLY ||                MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.EXACTLY;        mMatchParentChildren.clear();        int maxHeight = 0;        int maxWidth = 0;        int childState = 0;    //这里遍历子view依次进行测量        for (int i = 0; i < count; i++) {            final View child = getChildAt(i);            if (mMeasureAllChildren || child.getVisibility() != GONE) {            //这里进行子view的测量,注意参数usedWidth与usedHeight参数都为0 ,与LinearLayout和RelativeLayout不同(不需要考虑已经被其它孩子占用的空间)                measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);                //child测量完成                final LayoutParams lp = (LayoutParams) child.getLayoutParams();                //记录宽子view高最大值(等子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());                if (measureMatchParentChildren) {                //记录下宽高是match_parent的子view,后面还要重新测量                    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();        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;                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);                }                child.measure(childWidthMeasureSpec, childHeightMeasureSpec);            }        }    }
0 0
原创粉丝点击