1、理解MeasureSpec
MeasureSpec 很大程度上決定了一個View的尺寸規(guī)格。 之所以說是很大程度是因為這個過程還受父容器的影響, 因為父容器影響View的MeasureSpec的創(chuàng)建過程。在測量中系統(tǒng)會將View的Layoutparams根據父容器所施加的規(guī)則轉化成對應的MeasureSpec。 然后在根據這個measureSpec來測量View的寬高。 這里不一定等于View的最終寬高。
public static class MeasureSpec {
private static final int MODE_SHIFT = 30;
private static final int MODE_MASK = 0x3 << 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 = 0 << 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 = 1 << 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 = 2 << MODE_SHIFT;
/**
* Creates a measure specification based on the supplied size and mode.
*
* The mode must always be one of the following:
* <ul>
* <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
* <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
* <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
* </ul>
*
* <p><strong>Note:</strong> On API level 17 and lower, makeMeasureSpec's
* implementation was such that the order of arguments did not matter
* and overflow in either value could impact the resulting MeasureSpec.
* {@link android.widget.RelativeLayout} was affected by this bug.
* Apps targeting API levels greater than 17 will get the fixed, more strict
* behavior.</p>
*
* @param size the size of the measure specification
* @param mode the mode of the measure specification
* @return the measure specification based on size and mode
*/
public static int makeMeasureSpec(int size, int mode) {
if (sUseBrokenMakeMeasureSpec) {
return size + mode;
} else {
return (size & ~MODE_MASK) | (mode & MODE_MASK);
}
}
/**
* Like {@link #makeMeasureSpec(int, int)}, but any spec with a mode of UNSPECIFIED
* will automatically get a size of 0. Older apps expect this.
*
* @hide internal use only for compatibility with system widgets and older apps
*/
public static int makeSafeMeasureSpec(int size, int mode) {
if (sUseZeroUnspecifiedMeasureSpec && mode == UNSPECIFIED) {
return 0;
}
return makeMeasureSpec(size, mode);
}
/**
* Extracts the mode from the supplied measure specification.
*
* @param measureSpec the measure specification to extract the mode from
* @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
* {@link android.view.View.MeasureSpec#AT_MOST} or
* {@link android.view.View.MeasureSpec#EXACTLY}
*/
public static int getMode(int measureSpec) {
return (measureSpec & MODE_MASK);
}
/**
* Extracts the size from the supplied measure specification.
*
* @param measureSpec the measure specification to extract the size from
* @return the size in pixels defined in the supplied measure specification
*/
public static int getSize(int measureSpec) {
return (measureSpec & ~MODE_MASK);
}
...
}
View的三種測量模式:
UNSPECIFIED:父容器不對View有任何限制,要多大給多大。
EXACTLY: 父容器已經檢測出View所需要的精確大小,這個時候View的大小就是SpecSIze所指定的值。他對應于LayoutParams中的match_parent和具體的數值這兩種模式。
AT_MOST:父容器指定了一個可用大小SpecSize。View的大小不能超過這個值,它對應于LayoutParams的wrap_content。(例如:父布局width或者height設置一個具體值,或者match_parent,子布局設置為wrap_content。此時子布局的最大width或者height就是父布局的width或者height)。使用這種測量模式的View,設置的一定是wrap_content。
2.MeasureSpec 和 LayoutParams的關系
系統(tǒng)內部是通過MeasureSpec來進行View的測量。 在View測量時,系統(tǒng)將LayoutParams在父容器的的約束下轉換成對應的MeasureSpec,然后再根據這個MeasureSpec來確定View測量后的寬高。MeasureSpec不是唯一由LayoutParams決定的. LayoutParams需要和父容器一起才能決定View的MeasureSpec。從而進一步決定View的寬高。 對于普通View,MeasureSpec是由父容器的MeasureSpec和自身的Layoutparams來共同決定。 MeasureSpec一旦確定后,onMeasure中就可以確定View的測量寬高。普通的View的measure過程由ViewGroup傳遞而來。
以下下是ViewGroup的measureChildWidthMargins方法:
protected void measureChildWithMargins(View child,int parentWidthMeasureSpec, int widthUsed,
int parentHeightMeasureSpec, int heightUsed) {
final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin
+ widthUsed, lp.width);
final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin
+ heightUsed, lp.height);
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}
上述方法對子元素進行measure,在調用子元素的measure方法前先通過getChildMeasureSpec來得到子元素的MeasureSpec。 從代碼來看。子元素的MeasureSpec的創(chuàng)建好父容器的MeasureSpec和自己本身的LayoutParams有關。此外還和View的margin和padding有關。
具體情況可以看下ViewGroup的getChildMeasuerSpec的方法。
來自博客:http://m.itdecent.cn/p/e43a78deab86
public static int getChildMeasureSpec(int spec, int padding, int childDimension) {
int specMode = MeasureSpec.getMode(spec); //父控件的測量模式
int specSize = MeasureSpec.getSize(spec); //父控件的測量大小
int size = Math.max(0, specSize - padding);
int resultSize = 0;
int resultMode = 0;
switch (specMode) {
// 當父控件的測量模式 是 精確模式.
case MeasureSpec.EXACTLY:
//如果child的布局參數有固定值,比如"layout_width" = "100dp"
//那么顯然child的測量規(guī)格也可以確定下來了,測量大小就是100dp,測量模式也是EXACTLY
if (childDimension >= 0) {
resultSize = childDimension;
resultMode = MeasureSpec.EXACTLY;
}
//如果child的布局參數是"match_parent",也就是想要占滿父控件
//而此時父控件是精確模式,也就是能確定自己的尺寸了,那child也能確定自己大小了
else if (childDimension == LayoutParams.MATCH_PARENT) {
resultSize = size;
resultMode = MeasureSpec.EXACTLY;
}
//如果child的布局參數是"wrap_content",也就是想要根據自己的邏輯決定自己大小,
//比如TextView根據設置的字符串大小來決定自己的大小
//那就自己決定唄,不過你的大小肯定不能大于父控件的大小嘛
//所以測量模式就是AT_MOST,測量大小就是父控件的size
else if (childDimension == LayoutParams.WRAP_CONTENT) {
resultSize = size;
resultMode = MeasureSpec.AT_MOST;
}
break;
// 當父控件的測量模式 是 最大模式,也就是說父控件自己還不知道自己的尺寸,但是大小不能超過size
case MeasureSpec.AT_MOST:
//同樣的,既然child能確定自己大小,盡管父控件自己還不知道自己大小,也優(yōu)先滿足孩子的需求
if (childDimension >= 0) {
resultSize = childDimension;
resultMode = MeasureSpec.EXACTLY;
}
//child想要和父控件一樣大,但父控件自己也不確定自己大小,所以child也無法確定自己大小
//但同樣的,child的尺寸上限也是父控件的尺寸上限size
else if (childDimension == LayoutParams.MATCH_PARENT) {
resultSize = size;
resultMode = MeasureSpec.AT_MOST;
}
//child想要根據自己邏輯決定大小,那就自己決定唄
else if (childDimension == LayoutParams.WRAP_CONTENT) {
resultSize = size;
resultMode = MeasureSpec.AT_MOST;
}
break;
// Parent asked to see how big we want to be
case MeasureSpec.UNSPECIFIED:
if (childDimension >= 0) {
// 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 = 0;
resultMode = MeasureSpec.UNSPECIFIED;
} else if (childDimension == LayoutParams.WRAP_CONTENT) {
// Child wants to determine its own size.... find out how
// big it should be
resultSize = 0;
resultMode = MeasureSpec.UNSPECIFIED;
}
break;
}
return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
}
以上方法的理解:根據父容器的MeasureSpec同時結合View的本身LayoutParams來確定子元素的MeasureSpec。 參數中的padding是指父容器中已經占用的空間大小。因此元素可用的大小為父容器的尺寸減去padding。
將以上方法整理成表格:

注意, 當View采用固定寬高時, 不管富容器的MeasureSpec是什么, View的MeasureSpec都是EXACTLY,并且其大小遵循LayoutParams中的大小。 當View的寬高是match_parent時。如果父容器的模式是EXACTLY那么View也是EXACTLY, 其大小是父容器剩余空間。如果父容器是AT_MOST。那么View也是AT_MOST,并且最大值不會超過父容器的剩余空間。當View的寬高是wrap_content時。無論父容器是exactly還是at_most。View總是at_most