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 = 30;
/**
* 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;
如上所示,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 = 0; 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(0, specSize - padding);
int resultSize = 0;
int resultMode = 0;
switch (specMode) {
// Parent has imposed an exact size on us
case MeasureSpec.EXACTLY:
if (childDimension >= 0) {
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 >= 0) {
// 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 >= 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 = 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);
}
上面的方法的逻辑很清晰,就是根据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。