天天看點

MeasureSpec和LayoutParamsLayoutParams和MeasureSpec

LayoutParams和MeasureSpec

說到View的Measure就離不開MeasureSpec。那麼MeasureSpec到底是什麼呢?它與我們熟悉的LayoutParams有什麼聯系呢?

簡單的說:

  • MeasureSpec是一個int值;
  • MeasureSpec決定了View的measuredWidth/Height;
  • 父容器的MeasureSpec與子View的LayoutParams一同決定了子View的MeasureSpec。
下面我們詳細看一下它們是什麼。

LayoutParams

LayoutParams是View布局資訊的載體,它包含了View應有的width、height等資訊。一般來說,不同的ViewGroup都會實作自己的LayoutParams子類,用于支援額外的屬性,比如margin、weight、gravity等。

當我們在xml檔案裡定義一個View時,我們為它指定的屬性會被裝載到AttributeSet傳遞給View,并在inflate的時候通過generateLayoutParams(AttributeSet)方法生成相應的LayoutParams。

我們也可以在代碼中動态定義一個View,或者動态更新View的LayoutParams,像下面這樣:

TextView textView = new TextView(this);
    textView.setText("TextView");
    textView.setLayoutParams(
      new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT ) );
           

MeasureSpec

MeasureSpec直譯為“測量規格“,它作為View的測量的限制條件,直接影響View的測量結果。為了減少對象配置設定,MeasureSpec被實作為一個32位int值,該int值由兩部分組成:

  • 0~29:SpecSize,規格尺寸;
  • 30~31:SpecMode,測量模式。
private static final int MODE_SHIFT = ;
    /**
     * Measure specification mode: The parent has not imposed any constraint
     * on the child. It can be whatever size it wants.
     */
    public static final int UNSPECIFIED =  << MODE_SHIFT;

    /**
     * Measure specification mode: The parent has determined an exact size
     * for the child. The child is going to be given those bounds regardless
     * of how big it wants to be.
     */
    public static final int EXACTLY     =  << MODE_SHIFT;

    /**
     * Measure specification mode: The child can be as large as it wants up
     * to the specified size.
     */
    public static final int AT_MOST     =  << MODE_SHIFT;
           

如上所示,Measure中定義的測量模式共有UNSPECIFIED、EXACTLY以及AT_MOST三種。注釋中對三種測量模式的描述很清楚,它們分别表示:

  • UNSPECIFIED:表示父容器不對子View做任何限制;
  • EXACTLY:表示父容器已經确定了子View的尺寸,子View的尺寸就是SpecSize;
  • AT_MOST:表示父容器為子View指定了一個最大尺寸SpecSize,子View最大不能超過該尺寸;

為了友善,Measure類中提供了數個靜态方法用于從int值中分離SpecMode、SpecSize以及将SpecMode、SpecSize打包成一個int值。

LayoutParams與MeasureSpec的關系

現在,我們了解了LayoutParams和MeasureSpec,那麼MeasureSpec是如何生成的呢?它與LayoutParams又有什麼關系呢?

在View的測量過程中,父容器會結合自己的MeasureSpec以及子View的LayoutParams,生成子View的MeasureSpec,然後再在此MeasureSpec的限制下對子View進行測量。那麼,父容器的MeasureSpec、子View的LayoutParams與生成的子View 的MeasureSpec是怎麼的關系呢?

簡單的說,父容器在自己的MeasureSpec和子View的LayoutParams的共同作用下生成子View的MeasureSpec,然後子View在MeasureSpec限制下進行自己的測量。該過程自然要發生在measure階段,并且子View在父容器執行onMeasure時被測量,而ViewGroup沒有提供onMeasure的實作(因為不同的ViewGroup的measure過程是不同的),但是ViewGroup提供了measureChildren方法,下面看一下measureChildren:

protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) {
        final int size = mChildrenCount;
        final View[] children = mChildren;
        for (int i = ; i < size; ++i) {
            final View child = children[i];
            if ((child.mViewFlags & VISIBILITY_MASK) != GONE) {
                measureChild(child, widthMeasureSpec, heightMeasureSpec);
            }
        }
    }
           

measureChildren方法簡單的周遊子View,然後調用measureChild方法,下面看一下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);
    }
           

measureChild方法裡通過getChildMeasureSpec方法生成子View的MeasureSpec,然後調用子View的measure方法對子View進行測量,下面看一下getChildMeasureSpec方法:

public static int getChildMeasureSpec(int spec, int padding, int childDimension) {
        int specMode = MeasureSpec.getMode(spec);
        int specSize = MeasureSpec.getSize(spec);

        int size = Math.max(, specSize - padding);

        int resultSize = ;
        int resultMode = ;

        switch (specMode) {
        // Parent has imposed an exact size on us
        case MeasureSpec.EXACTLY:
            if (childDimension >= ) {
                resultSize = childDimension;
                resultMode = MeasureSpec.EXACTLY;
            } else if (childDimension == LayoutParams.MATCH_PARENT) {
                // Child wants to be our size. So be it.
                resultSize = size;
                resultMode = MeasureSpec.EXACTLY;
            } else if (childDimension == LayoutParams.WRAP_CONTENT) {
                // Child wants to determine its own size. It can't be
                // bigger than us.
                resultSize = size;
                resultMode = MeasureSpec.AT_MOST;
            }
            break;

        // Parent has imposed a maximum size on us
        case MeasureSpec.AT_MOST:
            if (childDimension >= ) {
                // Child wants a specific size... so be it
                resultSize = childDimension;
                resultMode = MeasureSpec.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;
                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;
                resultMode = MeasureSpec.AT_MOST;
            }
            break;

        // Parent asked to see how big we want to be
        case MeasureSpec.UNSPECIFIED:
            if (childDimension >= ) {
                // Child wants a specific size... let him have it
                resultSize = childDimension;
                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 ?  : 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 ?  : size;
                resultMode = MeasureSpec.UNSPECIFIED;
            }
            break;
        }
        //noinspection ResourceType
        return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
    }
           

上面的方法的邏輯很清晰,就是根據specMode以及來自LayoutParams的childDimension生成相應的MeasureSpec。childDimension有三類值,分别是MATCH_PARENT、WRAP_CONTENT以及确定的尺寸值。總的來說,子View的specMode有下面的對應關系:

MATCH_PARENT WRAP_CONTENT 準确的尺寸值
EXACTLY EXACTLY AT_MOST
AT_MOST AT_MOST AT_MOST
UNSPECIFIED UNSPECIFIED UNSPECIFIED

在childDimension的值為MATCH_PARENT或者WRAP_CONTENT時,specSize的值為size(UNSPECIFIED模式為0或者size),也即父容器specSize的值減去padding。childDimension的值為準确的尺寸值時,specSize = childDimension。