在Android中,写Layout文件时,用到最多的就是FrameLayout,LinearLayout,Relative,今天我们来分析一下
特点:
1)FrameLayout 是层级布局,即所有的子View都是从底层向上层开始,默认不指定margin或者layout_gravity的话,每个子view的坐标其实坐标都是Framelayout的其实坐标
2)LinearLayout 线性布局,分为水平方向和垂直方向, 水平方向是从左到右开始,垂直方向是从上到下开始,复杂的布局情况下,可能会出线很多的嵌套
3)RelativeLayout 相对布局,每个子View都是相对父布局或其他的子View进行位置的安放,一定程度上,可减少布局层级嵌套
下面我们从源码的角度来分析这三种布局
上面三种布局,归根结底还是ViewGroup,它们的作用主要是用来测量子view大小,设置子view的显示坐标,以及绘制子view,
它们在绘制这一部分没有区别,所有的区别都在mearsure和layout过程,下面我们就针对这三种布局的mearsure和layout进行分析
1) FrameLayout
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int count = getChildCount();
//判断FrameLayout 宽 高 是否同时设置了Match_parent或者 设置了指定大小的宽高
// measureMatchParentChildren 为 true,则表示没有设置了Match_parent或者 设置了指定大小的宽高
final boolean measureMatchParentChildren =
MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.EXACTLY ||
MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.EXACTLY;
mMatchParentChildren.clear();
int maxHeight = 0;
int maxWidth = 0;
int childState = 0;
//下面这个循环就是计算出所有的子View中的最大宽度 maxWidth 和最大高度 maxHeight
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (mMeasureAllChildren || child.getVisibility() != GONE) {
// 计算子View的宽高,包含了子View的左右上下Margin,
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());
// FrameLayout没有设置固定大小的宽高或Match_parent高宽,则收集所有设置了Match_parent的子View
if (measureMatchParentChildren) {
if (lp.width == LayoutParams.MATCH_PARENT ||
lp.height == LayoutParams.MATCH_PARENT) {
mMatchParentChildren.add(child);
}
}
}
}
// 加上左右 上下的Padding
// Account for padding too
maxWidth += getPaddingLeftWithForeground() + getPaddingRightWithForeground();
maxHeight += getPaddingTopWithForeground() + getPaddingBottomWithForeground();
// Check against our minimum height and width
maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight());
maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());
// Check against our foreground's minimum height and width
final Drawable drawable = getForeground();
if (drawable != null) {
maxHeight = Math.max(maxHeight, drawable.getMinimumHeight());
maxWidth = Math.max(maxWidth, drawable.getMinimumWidth());
}
//设置FrameLayout的宽高
setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, childState),
resolveSizeAndState(maxHeight, heightMeasureSpec,
childState << MEASURED_HEIGHT_STATE_SHIFT));
// 如果FrameLayout没有同时设置固定大小或者Match_parent宽高,且有子View设置了Match_parent宽高,
//则重新测量设置了Match_Parent宽高的子View
count = mMatchParentChildren.size();
if (count > 1) {
for (int i = 0; i < count; i++) {
final View child = mMatchParentChildren.get(i);
final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
final int childWidthMeasureSpec;
if (lp.width == LayoutParams.MATCH_PARENT) {
final int width = Math.max(0, getMeasuredWidth()
- getPaddingLeftWithForeground() - getPaddingRightWithForeground()
- lp.leftMargin - lp.rightMargin);
childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(
width, MeasureSpec.EXACTLY);
} else {
childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec,
getPaddingLeftWithForeground() + getPaddingRightWithForeground() +
lp.leftMargin + lp.rightMargin,
lp.width);
}
final int childHeightMeasureSpec;
if (lp.height == LayoutParams.MATCH_PARENT) {
final int height = Math.max(0, getMeasuredHeight()
- getPaddingTopWithForeground() - getPaddingBottomWithForeground()
- lp.topMargin - lp.bottomMargin);
childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
height, MeasureSpec.EXACTLY);
} else {
childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec,
getPaddingTopWithForeground() + getPaddingBottomWithForeground() +
lp.topMargin + lp.bottomMargin,
lp.height);
}
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}
}
}
总结一下FrameLayout的Mearsure过程:
1)测量FrameLayout的宽高,具体测量过程如下
计算出所有的子View中最大的宽,高,因为FrameLayout中的每个View 在不同的层级,所有测量是,需要计算上 左右,上下Margin
最大的宽,高 加上 FrameLayout自身的上下,左右Padding,即组成了FrameLayout实际需要设置的宽,高
2) 如果FragmeLayout本身宽,高不是固定大小或者Match_Parent,需要重新计算宽高 设置了Match_Parent 的子View的大小
FrameLayout Layout过程
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
layoutChildren(left, top, right, bottom, false /* no force left gravity */);
}
void layoutChildren(int left, int top, int right, int bottom, boolean forceLeftGravity) {
final int count = getChildCount();
final int parentLeft = getPaddingLeftWithForeground();
final int parentRight = right - left - getPaddingRightWithForeground();
final int parentTop = getPaddingTopWithForeground();
final int parentBottom = bottom - top - getPaddingBottomWithForeground();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() != GONE) {
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
final int width = child.getMeasuredWidth();
final int height = child.getMeasuredHeight();
int childLeft;
int childTop;
int gravity = lp.gravity;
if (gravity == -1) {
gravity = DEFAULT_CHILD_GRAVITY;
}
final int layoutDirection = getLayoutDirection();
final int absoluteGravity = Gravity.getAbsoluteGravity(gravity, layoutDirection);
final int verticalGravity = gravity & Gravity.VERTICAL_GRAVITY_MASK;
// 根据水平方向Gravity,计算View的左右,坐标
switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
case Gravity.CENTER_HORIZONTAL:
childLeft = parentLeft + (parentRight - parentLeft - width) / 2 +
lp.leftMargin - lp.rightMargin;
break;
case Gravity.RIGHT:
if (!forceLeftGravity) {
childLeft = parentRight - width - lp.rightMargin;
break;
}
case Gravity.LEFT:
default:
childLeft = parentLeft + lp.leftMargin;
}
// 根据垂直方向Gravity,计算View的上下,坐标
switch (verticalGravity) {
case Gravity.TOP:
childTop = parentTop + lp.topMargin;
break;
case Gravity.CENTER_VERTICAL:
childTop = parentTop + (parentBottom - parentTop - height) / 2 +
lp.topMargin - lp.bottomMargin;
break;
case Gravity.BOTTOM:
childTop = parentBottom - height - lp.bottomMargin;
break;
default:
childTop = parentTop + lp.topMargin;
}
child.layout(childLeft, childTop, childLeft + width, childTop + height);
}
}
}
总结: FrameLayout的Layout过程非常简单,因为每个view在不同的层级,所以只需要计算每个View的Layout_gravity,
默认的Layout_gravity 为Gravity_left | GravityTop, 在根据Gravity计算时,分别通过水平方向的Gravity计算出 左右 坐标,然后根据垂直方向的Gravity计算出上下坐标。