视图的宽高可以灵活变化
android对于的视图布局的定义提供了一种很灵活的实现,就是当给视图的宽高属性设置wrap_content/match_parent,视图可以根据所处布局的情况动态的改变宽高。
wrap_content:根据自视图的内容决定视图大小
match_parent:占满父视图空间
wrap_content/match_parent有什么奥秘能够使视图的宽高灵活变化呢?
measureSpec影响宽高的计算
一个视图的绘制过程主要有三个部分:测量(measure),布局(layout)、绘制(draw)。其中测量过程就是计算一个视图宽高的过程,主要通过onMeasure方法实现。
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
...
}
onMeasure方法接收来至父视图对子视图宽高的约束measureSpec,具体的视图会根据measureSpec来计算视图的宽高。
measureSpec的一些概念:
measureSpec是一个32位的值,其前两位表示测量的模式,后30位表示测量的大小。
测量模式有三种:
EXACTLY:父控件给子控件决定特定的大小。
AT_MOST:子控件至多达到指定大小的值。
UNSPECIFIED:父控件没有对子控件施加任何约束,子控件可以得到任意想要的大小。
android提供了相应的方法来处理measureSpec
// 获取测量模式
int specMode = MeasureSpec.getMode(measureSpec);
// 获取测量大小
int specSize = MeasureSpec.getSize(measureSpec);
// 通过大小和模式生成measureSpec
MeasureSpec.makeMeasureSpec(resultSize, resultMode);
例如 TextView的onMeasure实现:
// onMeasure的关键实现
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
if (widthMode == MeasureSpec.EXACTLY) {
// Parent has told us how big to be. So be it.
width = widthSize;
} else {
...
if (widthMode == MeasureSpec.AT_MOST) {
width = Math.min(widthSize, width);
}
}
...
if (heightMode == MeasureSpec.EXACTLY) {
// Parent has told us how big to be. So be it.
height = heightSize;
mDesiredHeightAtMeasure = -1;
} else {
...
if (heightMode == MeasureSpec.AT_MOST) {
height = Math.min(desired, heightSize);
}
}
...
setMeasuredDimension(width, height);
}
首先:
从父视图的约束measureSpec获得约束的模式specMode和约束的大小specSize。
其次:
如果模式为EXACTLY(父控件给子控件决定特定的大小),则TextView的宽高直接为父视图约束的宽高;
如果模式是AT_MOST(子控件至多达到指定大小的值),则取TextView自身大小与父视图约束大小的最小值;
如果为UNSPECIFIED(父控件没有对子控件施加任何约束,子控件可以得到任意想要的大小),TextView没有额外处理就是取TextView自身的大小。
因此,视图宽高的计算是由父视图传递的约束measureSpec决定的。
那么measureSpec又是怎么来的呢?
wrap_content/match_parent影响measureSpec的计算
父视图在自己测量时会调用子视图的onMeasure方法,测量子视图
例如下面的一个布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!" />
</LinearLayout>
其测量的时序图:
在measureChildWithMargins方法中调用了子视图的measure方法,最终执行了子视图的onMeasure方法。
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);
}
而子视图onMeasure方法所需要的measureSpec就是由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);
在getChildMeasureSpec方法中终于见到了久违的wrap_content\match_parent,可以看出子视图的measureSpec由两部分决定,父视图的measureSpec和子视图的LayoutParams。
如果子视图的LayoutParams宽高设置的是具体的大小
...
if (childDimension >= 0) {
resultSize = childDimension;
resultMode = MeasureSpec.EXACTLY;
}
...
那么子视图meauserSpec约束模式就是EXACTLY,约束大小就是LayoutParams中定义的大小,根据前面介绍的TextView的onMeasure方法逻辑,计算出的大小就是约束的大小。
如果父视图约束的模式是EXACTLY(通常为父视图的宽高定义为具体大小),子视图LayoutParams中定义的是match_parent(占满父视图)
...
case MeasureSpec.EXACTLY:
if (childDimension == LayoutParams.MATCH_PARENT) {
// Child wants to be our size. So be it.
resultSize = size;
resultMode = MeasureSpec.EXACTLY;
}
...
得到的子视图的measureSpec约束模式还是EXACTLY,约束大小为父视图约束的大小,TextView计算的宽高则与父视图一样大,实现占满父视图的效果;
如果子视图定义为wrap_content
...
case MeasureSpec.EXACTLY:
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;
}
...
得到的子视图的measureSpec约束模式为AT_MOST,约束大小为父视图约束的大小,TextView计算的宽高则是自身大小与父视图约束大小的最小值一样大,实现由自身大小决定宽高,但不会超过父视图约束大小的效果。
还有很多种组合方式,就不一一列举,可以看到我们平常记忆理解的wrap_content\match_parent灵活变化视图宽高的作用效果,正是主要通过这段代码逻辑实现的。
理解视图测量时的一些疑问:
顶层视图的measureSpec的由来
从前面的讨论可以知道,测量方法的参数measureSpec都是由父视图传递进来的,那么最顶层的视图的measureSpec是从哪来的?
我们经常给activity设置的layout布局的根视图并非是最顶层视图。
先看一张图:
content部分为给activity设置的布局,其外层还有一个视图DecorView,这个才是最顶层视图。
而DecorView的测量过程是在ViewRootImpl的performTraversals方法中
private void performTraversals() {
...
//获得view宽高的测量规格,mWidth和mHeight表示窗口的宽高,lp.widthhe和lp.height表示 DecorView根布局宽和高
int childWidthMeasureSpec = getRootMeasureSpec(mWidth, lp.width);
int childHeightMeasureSpec = getRootMeasureSpec(mHeight, lp.height);
// Ask host how big it wants to be
//执行测量操作
performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
...
//执行布局操作
performLayout(lp, desiredWindowWidth, desiredWindowHeight);
...
//执行绘制操作
performDraw();
...
}
通过getRootMeasureSpec方法创建了DecorView的测量规格,在performMeasure方法中执行了DecorView的测量,遍历整个子视图。
并且视图的布局(layout)和绘制(draw)也是从这里发起的,通过先后执行performMeasure、performLayout、performDraw方法,完成整个页面的绘制过程
ListView对子视图测量的区别
ListView的子视图布局高度无论是设置wrap_content还是match_parent,其高度都为子视图自身的大小,这又是为什么呢?
ListView的中对子视图的测量调用的是其自己定义的方法measureScrapChild
private void measureScrapChild(View child, int position, int widthMeasureSpec, int heightHint) {
LayoutParams p = (LayoutParams) child.getLayoutParams();
...
if (lpHeight > 0) {
childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY);
} else {
childHeightSpec = MeasureSpec.makeSafeMeasureSpec(heightHint, MeasureSpec.UNSPECIFIED);
}
child.measure(childWidthSpec, childHeightSpec);
...
}
当子视图的高度设置为wrap_content或match_parent时,生成的子视图测量模式都是UNSPECIFIED,因此最终计算出的高度还是子视图自身的高度。