首先我们写一个TestView继承View,在布局中使用,代码很简单,看一下:
public TestView(Context context) {
super(context);
}
public TestView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public TestView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
}
布局中使用:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.test.applicationtest1017.TestView
android:background="@color/colorAccent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
可以看到属性 layout_width="wrap_content
",layout_height="wrap_content
"
但是看下展示效果是这样的(等同于match_parent
):
这是为什么呢?我们一起来看下原因(复习一下View的绘制流程,不熟悉的请自行度娘):
大家都知道View的绘制流程主要是三个方法:
1.onMeansure
2.onLayout
3.onDraw
这里主要说下onMeansure: 在View的测量过程中会将View的测量信息封装在一个MeansureSpec对象中,这个对象是一个32位的int值,其中前两位表示SpecMode(模式)
后30位表示SpecSize。
其中的SpecMode是定义好的三个常量,这三个常量的值和其对应的含义就是:
UNSPECIFIED
=00000000000000000000000000000000 表示: 父容器不对View做任何限制,系统内部使用EXACTLY
=01000000000000000000000000000000 表示: 父容器检测出View的大小,Vew的大小就是SpecSize LayoutPamras match_parent 固定大小AT_MOST
= 10000000000000000000000000000000 表示: 父容器指定一个可用大小,View的大小不能超过这个值,(对应LayoutPamras wrap_content)在ViewGroup的onMeasure方法中我们可以看到,会遍历递归子View,并调用measureChildWithMargins方法,并传入了父控件本身的
widthMeasureSpec
和heightMeasureSpec
,ViewGroup(以FrameLayout代码片段为例)的onMeasure方法如下
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int count = getChildCount();
final boolean measureMatchParentChildren =
MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.EXACTLY ||
MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.EXACTLY;
mMatchParentChildren.clear();
int maxHeight = 0;
int maxWidth = 0;
int childState = 0;
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (mMeasureAllChildren || child.getVisibility() != GONE) {
measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
maxWidth = Math.max(maxWidth,
child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin);
maxHeight = Math.max(maxHeight,
child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin);
childState = combineMeasuredStates(childState, child.getMeasuredState());
if (measureMatchParentChildren) {
if (lp.width == LayoutParams.MATCH_PARENT ||
lp.height == LayoutParams.MATCH_PARENT) {
mMatchParentChildren.add(child);
}
}
}
}
我们看下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);
}
可以看到这个方法主要是调用了getChildMeasureSpec
决定了子View的MeasureSpec和调用了child.measure
方法。
我们再看下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);
}
可以看到这个方法接收的第一个参数是父控件
的MeasureSpec,然后将父控件的MeasureSpec拆分为MeasreMode和MeasureSize并通过MeasreMode的值来暂时决定子View
的MeasureSpec和MeasureSize
也就是说子View的LayoutParams是会被父控件的MeasureSpec影响的:我们看下两者之间的对应关系对应
然后我们点进去的
child.measure
方法会来到View
的measure
方法,然后会调用View
的onMeasure
方法:
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
}
然后我们看到这里调用了一个getDefaultSize
方法,我们看下这个方法做了什么:
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:
case MeasureSpec.EXACTLY:
result = specSize;
break;
}
return result;
}
这里我们就看到了问题所在:不管MeasureSpec的Mode是AT_MOST
还是EXACTLY
返回的size都是同一个值
。这个值就是在getChildMeasureSpec
中由父控件的MeasureSpec和父控件的剩余空间决定的,
所以说我们在自定义View的时候有必要重写onMeasure
方法来重新测量控件的大小。
网友评论