View工作原理 -- 工作过程 -- measure(2)

ViewGroup的measure过程

对于ViewGroup来说,除了完成自己的measure过程以外,还会遍历去调用所有子元素的measure方法,各个子元素再递归去执行这个过程。和View不同的是,ViewGroup是一个抽象类,因此它没有重写View的onMeasure方法,但它提供一个measureChildren的方法。

//ViewGroup#measureChildren:
protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) { //widthMeasureSpec和heightMeasureSpec为当前ViewGroup的MeasureSpec
    final int size = mChildrenCount;
    final View[] children = mChildren;
    for(int i = 0; i < size; ++i) {
        final View child = children[i];
        if((child.mViewFlags & VISIBILITY_MASK) != GONE) {
            measureChild(child, widthMeasureSpec, heightMeasureSpec);
        }
    }
}

//ViewGroup#measureChild:
protected void measureChild(View child, int parentWidthMeasureSpec, int parentHeightMeasureSpec) { 
    final LayoutParams lp = child.getLayoutParams();
    final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec, mPaddingLeft + mPaddingRight, lp.width);
    final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec, mPaddingTop + mPaddingBottom, lp.height);
    child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}

ViewGroup并没有定义测量的具体过程,这是因为ViewGroup是一个抽象类,其测量过程的onMeasure方法需要各个子类去具体实现,比如LinearLayout、RelativeLayout等,不同的ViewGroup子类有不同的布局特性,这导致它们的测量细节各不相同,因此ViewGroup无法做统一实现。

分析LinearLayout:

LinearLayout#onMeasure:
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    if(mOrientation == VERTICAL) {
        measureVertical(widthMeasureSpec, heightMeasureSpec);
    } else {
        measureHorizontal(widthMeasureSpec, heightMeasureSpec);
    }
}

//LinearLayout#measureVertical:
for(int i = 0; i < count; ++i) {
    final View child = getVirtualChildAt(i);
    ...
    measureChildBeforeLayout(child, i, widthMeasureSpec, 0, heightMeasureSpec, totalWeight == 0 ? mTotalLength : 0);
    if(oldHeight != Integer.MIN_VALUE) {
        lp.height = oldHeight;
    }
    final int childHeight = child.getMeasuredHeight();
    final int totalLength = mTotalLength;
    mTotalLength = Math.max(totalLength, totalLength + childHeight + lp.topMargin + lp.bottomMargin + getNextLocationOffset(child));
}

系统会遍历子元素并对每个子元素执行measureChildBeforeLayout方法,这个方法内部会调用ViewGroup#measureChildWithMargins方法,进而调用View#measure方法,这样各个子元素就开始依次进入measure过程,并且系统会通过mTotalLength这个变量来存储LinearLayout在竖直方向的初步高度。

每测量一个子元素,mTotalLength就会增加,增加的部分主要包括了子元素的高度以及子元素在竖直方向上的margin等。当子元素测量完毕后,LinearLayout会测量自己的大小。
源码如下:

//LinearLayout#measureVertical:
mTotalLength += mPaddingTop + mPaddingBottom;
int heightSize = mTotalLength; //heightSize表示当前ViewGroup已占用的空间大小
heightSize = Math.max(heightSize, getSuggestedMinimumHeight());
int heightSizeAndState = resolveSizeAndState(heightSize, heightMeasureSpec, 0);
heightSize = heightSizeAndState & MEASURED_SIZE_MASK;
...
setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, childState), heightSizeAndState);

//LinearLayout#resolveSizeAndState:
public static int resolveSizeAndState(int size, int measureSpec, int child childMeasuredState) {
    int result = size; //size表示当前ViewGroup已占用的空间大小
    int specMode = MeasureSpec.getMode(measureSpec);
    int specSize = MeasureSpec.getSize(measureSpec); //specSize表示当前ViewGroup的测量规格大小
    switch(specMode) {
    case MeasureSpec.UNSPECIFIED:
        result = size; //当前ViewGroup已占用的空间大小
        break;
    case MeasureSpec.AT_MOST:
        if(specSize < size) { //如果 当前ViewGroup已占用的空间大小 大于 当前ViewGroup的测量规格大小 
            result = specSize | MEASURED_STATE_TOO_SMALL; //当前ViewGroup的测量规格大小 
        } else {
            result = size; //当前ViewGroup已占用的空间大小
        }
        break;
    case MeasureSpec.EXACTLY:
        result = specSize; //当前ViewGroup的测量规格大小
        break;
    }
    return result | (childMeasuredState & MEASURED_STATE_MASK);
}

当子元素测量完毕后,LinearLayout会根据子元素的情况来测量自己的大小。针对竖直的LinearLayout而言,它在水平方向的测量过程遵循View的测量过程,在竖直方向的测量过程则和View有所不同。
(1)如果布局高度采用match_parent或固定大小,那么它的测量过程和View一致,即高度为specSize。
(2)如果布局高度采用wrap_content,那么它的高度是所有子元素所占用的高度总和(当然还需要考虑其在竖直方向的padding),但是不能超过它的父容器的剩余空间(即当前ViewGroup的测量规格大小)。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容