聊一聊Android中View大小的确定

View # measure 方法的入参 widthMeasureSpecheightMeasureSpec 是在 measure 过程中父容器传递过来的,接下来看一下父容器是如何确定这两个值的。

public class View{
    public final void measure(int widthMeasureSpec, int heightMeasureSpec) 
}

measureSpec 使用int中的2位存储specMode,剩下的30位存储specSize

ViewGroup # measureChild :让其中的一个子View去测量自己。调用 child.measure 方法,我们着重来看一下 childWidthMeasureSpecchildHeightMeasureSpec 是如何确定的。

class public ViewGroup{   
    protected void measureChild(View child, int parentWidthMeasureSpec,
            int parentHeightMeasureSpec) {
        // 获取child的布局参数
        final LayoutParams lp = child.getLayoutParams();
        // 通过 ViewGroup自己的水平padding + ViewGroup自己的parentWidthMeasureSpec + 子View的width
        final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
                mPaddingLeft + mPaddingRight, lp.width);
        // 通过 ViewGroup自己的垂直padding + ViewGroup自己的parentHeightMeasureSpec + 子View的height
        final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
                mPaddingTop + mPaddingBottom, lp.height);

        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
    }
}

以 传入child 的childWidthMeasureSpec为例,它是由 ViewGroup 水平的padding + ViewGroup的parentWidthMeasureSpec + 子View的 width 共同决定。这三个者的融合方式是 getChildMeasureSpec 来决定的。

public static int getChildMeasureSpec(int spec, int padding, int childDimension) {
        int specMode = MeasureSpec.getMode(spec);
        int specSize = MeasureSpec.getSize(spec);
  
        // size 为父容器的宽/高 - 父容器的padding
        int size = Math.max(0, specSize - padding);

        int resultSize = 0;
        int resultMode = 0;

        switch (specMode) {
        // Parent has imposed an exact size on us
        case MeasureSpec.EXACTLY:   // 如果父容器是 EXACTLY
            if (childDimension >= 0) {     // 子View的宽/高 设定了固定的值
                resultSize = childDimension;  //size就直接使用子view自己设定的值
                resultMode = MeasureSpec.EXACTLY;  //mode就是EXACTLY
            } else if (childDimension == LayoutParams.MATCH_PARENT) {    // 子View是match_parent
                // Child wants to be our size. So be it.
                resultSize = size;   // size就是父容器的size
                resultMode = MeasureSpec.EXACTLY;  // mode 是 EXACTLY
            } else if (childDimension == LayoutParams.WRAP_CONTENT) {
                // Child wants to determine its own size. It can't be
                // bigger than us.
                resultSize = size;   // size就是父容器的size
                resultMode = MeasureSpec.AT_MOST;  // mode 是 AT_MOST
            }
            break;

        // Parent has imposed a maximum size on us
        case MeasureSpec.AT_MOST:     // 如果父容器是 AT_MOST
            if (childDimension >= 0) {     // 子View的宽/高 设定了固定的值
                // Child wants a specific size... so be it
                resultSize = childDimension;    //size就直接使用子view自己设定的值
                resultMode = MeasureSpec.EXACTLY;  //mode就是EXACTLY
            } else if (childDimension == LayoutParams.MATCH_PARENT) {
                // Child wants to be our size, but our size is not fixed.
                // Constrain child to not be bigger than us.
                resultSize = size;     // size就是父容器的size
                resultMode = MeasureSpec.AT_MOST;
            } else if (childDimension == LayoutParams.WRAP_CONTENT) {
                // Child wants to determine its own size. It can't be
                // bigger than us.
                resultSize = size;     // size就是父容器的size
                resultMode = MeasureSpec.AT_MOST;
            }
            break;

        // Parent asked to see how big we want to be
        case MeasureSpec.UNSPECIFIED:
            if (childDimension >= 0) {  // 子View的宽/高 设定了固定的值
                // Child wants a specific size... let him have it
                resultSize = childDimension;   //size就直接使用子view自己设定的值
                resultMode = MeasureSpec.EXACTLY;
            } else if (childDimension == LayoutParams.MATCH_PARENT) {
                // Child wants to be our size... find out how big it should
                // be
                resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;
                resultMode = MeasureSpec.UNSPECIFIED;
            } else if (childDimension == LayoutParams.WRAP_CONTENT) {
                // Child wants to determine its own size.... find out how
                // big it should be
                resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;
                resultMode = MeasureSpec.UNSPECIFIED;
            }
            break;
        }
        //noinspection ResourceType
        return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
    }

这几种组合方式有一个特点:

  • 如果子View本身的width / height是确定的值(非MATCH_PARENT、WRAP_CONTENT)那么,无论父View是何种spec,子view最终测量的结果的size就是的width / height,mode为EXACTLY。
  • 父View 的spec为EXACTLY,如果子View的width / height 是MATCH_PARENT,测量的size 就是父容器的 width / height, mode 是 EXACTLY;如果子View的width / height 是WRAP_CONTENT,测量的size 就是父容器的 width / height,mode 是AT_MOST。
  • 父View 的spec为AT_MOST,如果子View的width / height 是MATCH_PARENT,测量的size 就是父容器的 width / height, mode 是 AT_MOST;如果子View的width / height 是WRAP_CONTENT,测量的size 就是父容器的 width / height,mode 是AT_MOST。
  • 父View 的spec为UNSPECIFIED,如果子View的width / height 是MATCH_PARENT,测量的size 就是父容器的 width / height, mode 是 UNSPECIFIED;如果子View的width / height 是WRAP_CONTENT,测量的size 就是父容器的 width / height,mode 是 UNSPECIFIED。

小结:传给子View的 widthMeasureSpec / heightMeasureSpec 的值已经是它的父容器根据父容器 和 子View的layoutParams 综合得出的测量结果。

那么子View 对于父容器传来的 widthMeasureSpec/heightMeasureSpec 是如何处理的呢?

public class View{
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // 设置测量的结果
        setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
                getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
    }
   
    public static int getDefaultSize(int size, int measureSpec) {
        int result = size;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        switch (specMode) {
        case MeasureSpec.UNSPECIFIED:    // 如果是UNSPECIFIED
            result = size;    // 则最终的值为最小的width/height
            break;
        case MeasureSpec.AT_MOST:
        case MeasureSpec.EXACTLY:
            result = specSize;  // 父容器建议的width / height
            break;
        }
        return result;
    }
}

总的来说 父容器传来的 widthMeasureSpec/heightMeasureSpec 是一个建议值或者参考值,子View默认的处理逻辑是:如果spec是 UNSPECIFIED,那么最终的size 则忽略测量的size,直接使用子View的 minimumWidth / minimumHeight; 如果spec 是 AT_MOST、EXACTLY,那么直接使用父容器建议的size。

当然我们也可以重写onMeasure,完全忽略父容器的建议,直接使用任意的值。

结论:
1、传递给子View的 widthMeasureSpec 和 heightMeasureSpec 是根据父容器 和 子View 的综合因素所决定。
2、子View对于父View传过来的 widthMeasureSpec 和 heightMeasureSpec 建议值,会根据 spec 做不同的选择。

接收到父控件传递的MeasureSpec后,View应该如何用来处理自己的尺寸呢?onMeasure是View测量尺寸最合理的时机,如果View不是ViewGroup相对就比较简单,只需要参照MeasureSpec,并跟自身需求来设定尺寸即可,默认onMeasure的就是完全按照父控件传递MeasureSpec设定自己的尺寸的。这里重点讲一下ViewGroup,为了获得合理的宽高尺寸,ViewGroup在计算自己尺寸的时候,必须预先知道所有子View的尺寸,然后综合MeasureSpec跟自身需求,得出自己合理的尺寸。

关于UNSPECIFIED,这里有一个解答 https://www.cnblogs.com/liushilin/p/11055741.html

你可能感兴趣的:(聊一聊Android中View大小的确定)