view显示到屏幕上,大概来讲,需要走如下的流程:从调用方法的角度来看,即需要走三个方法:measure(),layout(),draw(),这三个方法是view的方法,走完这三个方法view就会显示到屏幕上
而且这三个方法是final修饰的,系统不允许我们重写这些方法,要求我们遵循这个框架流程,但是又不能不让我们自定义自己的测量,布局和绘画过程,所以给出了onMeasure(),onLayout(),onDraw()可重写的方法,它们分别对应于measure(),layout(),draw()方法,分别在measure(),layout(),draw()内部调用,(这里用到了模板方法模式)我们可以通过重写on开头的这些方法达到自定义的效果。
下面,看一下view的onMeasure()方法的默认实现,读代码发现默认实现wrap_content和match_parent获取的测量值是一样的,即都是通过int specSize = MeasureSpec.getSize(measureSpec);获取的,那么这里的measureSpec(即onMeasure(int widthMeasureSpec, int heightMeasureSpec)方法传过来的的两个参数,widthMeasureSpec 和 heightMeasureSpec从哪里来的呢?发现,是通过如下流程传过来的,从父容器开始,在父容器中的measure()方法中调用了measureChildren()方法,在measureChildren()方法中遍历了所有的子view,调用了measureChild()方法,在measureChild()方法中getChildMeasureSpec()方法获取了子View的大小和测量模式,然后再通过调用子view的measure()传给了子view的onMeasure()方法。简述如下
父measure() --> 父measureChildren()循环调用 --> 父measureChild() -- > 父 getChildMeasureSpec()-- > 子measure() --> 子onMeasure()
另外附上:画子view 的调用流程 :父draw() --> 父dispatchDraw() 循环调用 --> drawChild() --> 子draw()
上面的方法的调用在具体的控件中可能并不是准确的(具体到某个方法),但是整个流程是正确的,当把某个自定义view放到某个系统已经实现好的现有的父容器中时(比如Linear Layout,FrameLayout),由于父容器已经重写了ViewGroup的onMeasure()方法,加上了测量子view的逻辑(ViewGroup默认是没有对子View的操作的,只是提供了一些操作的方法,如measureChildren(),measureChild(),measureChildWithMargins()等),所以会调用getChildMeasureSpec()方法获取子view的大小和测量模式。
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//获取测量后的大小(getDefaultSize)
//并进行设置(setMeasuredDimension)
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:
result = size;
break;
case MeasureSpec.AT_MOST://对应wrap_content
case MeasureSpec.EXACTLY://对应match_parent和确定的高宽值
result = specSize;
break;
}
return result;
}
另:protected int getSuggestedMinimumWidth() {
return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
}
protected void measureChild(View child, int parentWidthMeasureSpec,
int parentHeightMeasureSpec) {
final LayoutParams lp = child.getLayoutParams();
//getChildMeasureSpec获取子View的测量规格
final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
mPaddingLeft + mPaddingRight, lp.width);
final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
mPaddingTop + mPaddingBottom, lp.height);
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}
//注:在LinearLayout中测量子view的方法调用的应该是下面这个方法(待验证)
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);
}
经过上面的分析结合 measureChild()方法发现getDefaultSize()方法参数中的 measureSpec而是通过getChildMeasureSpec()方法获取的,下面看一下getChildMeasureSpec()方法,可以看出,父容器是EXACTLY和AT_MOST时(UNSPECIFIED一般是系统使用,暂不考虑,无论子view是match_parent还是wrap_content,其获得的size(即方法中你的resultSize)都是父容器的size减去父容器自身的padding : int size = Math.max(0, specSize - padding);
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
//父容器是EXACTLY模式
case MeasureSpec.EXACTLY:
//子View 尺寸 >= 0,即指定了确切的宽高值。
if (childDimension >= 0) {
resultSize = childDimension;
resultMode = MeasureSpec.EXACTLY;
//子View是MATCH_PARENT
} else if (childDimension == LayoutParams.MATCH_PARENT) {
// Child wants to be our size. So be it.
resultSize = size;
resultMode = MeasureSpec.EXACTLY;
//子View是WRAP_CONTENT
} 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
//父容器是AT_MOST模式
case MeasureSpec.AT_MOST:
//子View 尺寸 >= 0,即指定了确切的宽高值。
if (childDimension >= 0) {
// Child wants a specific size... so be it
resultSize = childDimension;
resultMode = MeasureSpec.EXACTLY;
//子View是MATCH_PARENT
} 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;
//子View是WRAP_CONTENT
} 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
//父容器是UNSPECIFIED模式
case MeasureSpec.UNSPECIFIED:
//子View 尺寸 >= 0,即指定了确切的宽高值。
if (childDimension >= 0) {
// Child wants a specific size... let him have it
resultSize = childDimension;
resultMode = MeasureSpec.EXACTLY;
//子View是MATCH_PARENT
} 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;
//子View是WRAP_CONTENT
} 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);
}
解决办法:如下,即设置一个默认的宽高
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// 获取宽的大小
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
// 获取高的大小
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
//默认宽高大小
int defaultWidth = 300;
int defaultHeight = 300;
if (getLayoutParams().width == ViewGroup.LayoutParams.WRAP_CONTENT) {
widthSize = defaultWidth;
}
if (getLayoutParams().height == ViewGroup.LayoutParams.WRAP_CONTENT) {
heightSize = defaultHeight;
}
//重新设置大小
setMeasuredDimension(widthSize, heightSize);
}