*****这也是被问得比较多的一个点,它本身其实不难理解,但是MeasureSpeac参与到了View绘制,需要从整体来讨论它的作用*****
1. 概念
‘测量规格’,决定了View的尺寸,本质是一个32位int值,高两位是测量模式(SpeacMode),低30位是测量值(SpeacSize),内部源码将这两个值打包成一个值,提供了getMode和getSize方法。
SpeacMode 有三个值
UNSPECIFIED:父容器不做限制,View要多大给多大,一般是系统内部使用。
EXACTLY:父容器已经检测出View所需大小,具体值由SpeacSize决定。我们定义View的Layout_width,layout_height 为match_parent 或者具体数值时,就是EXACTLY。
AT_MOST:父容器不知道View要多大,根据View实际情况取值,但是最大不超过父容器的宽高。即使用wrap_content属性时,view的内容不确定,可以根据内容改变宽高值,但是最多只能和父容器一样大。
2.开发遇到的相关问题
自定义View中,在我们继承View的时候,会出现wrap_content属性失效。
这个问题一开始做自定义View的时候基本都会遇到,我们需要在onMeasure方法中给View指定一个默认宽高,在wrap_content时设置默认宽高就可以了;
造成这个原因就需要详细分析MeasureSpeac的原理了。
3.针对问题的原理分析
MeasureSpeac包含了两个值,它由父容器和View本身的LayoutParams共同决定,在ViewGroup中,有一个measureChildwithMargins方法
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之前获取了子元素的MeasureSpeac
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:
if (childDimension >= 0) {//子view 有具体dp/px值
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;
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;
......
return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
}
UNSPECIFIED时我们暂不考虑,根据这个方法我们可以总结一下View的MeasureSpec创建规则:
父View specMode为EXACTLY时:(1)子View layoutParams 有精确值,specMode为EXACTLY,specSize为具体值,最大不超过父View
(2) 子View LayoutParams为match_parant specMode为EXACTLY,specSize为 父容器size
(3)子View LayoutParams为wrap_content specMode为AT_MOST,specSize为 父容器size
父View specMode为AT_MOST时:(1)子View layoutParams 有精确值,specMode为EXACTLY,specSize为具体值,最大不超过父View
(2) 子View LayoutParams为match_parant specMode为AT_MOST,specSize为 父容器size
(3)子View LayoutParams为wrap_content specMode为AT_MOST,specSize为 父容器size
根据上面总结,在自定义View中,我们设置wrap_content属性时,默认的specSize都会是父容器的size,View得宽高等于父容器宽高,布局中效果与match_parant完全一致。这时候就需要针对具体情况为View指定一个默认宽高。在系统TextView等控件源码中,针对wrap_content 方法都作出了处理,可以自己看一下源码。