FrameLayout是一個ViewGroup。在ViewGroup最重要的兩步方法是測量和布局:onMeasure()、onLayout()方法。所以這里只分析FrameLayout的onMeasure()、onLayout()方法。
FrameLayout的特征:
FrameLayout的所有子View一般都是在容器的左上方,除非子View有設置layout_gravity這個屬性。
FrameLayout的onMeasure()源碼:
流程:FrameLayout的onMeasure(),會先遍歷所有子View,并為每個子View確定MeasureSpec,計算出子view中最大的寬度和高度,并把寬或高是match_parent的子view放進一個List集合中。把子view的最大寬高度+padding,然后調用setMeasuredDimension()方法,根據frameLayout的MeasureSpec和最大寬高度確認FrameLayout的大小。之后把剛剛match_parent的子view集合遍歷,根據已經確定了FrameLayout的大小再重新計算設置這個子View的MeasureSpec
private final ArrayList<View> mMatchParentChildren = new ArrayList<>(1);
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int count = getChildCount();
//是否大小已經確定,不需要子view的和大小來確定
final boolean measureMatchParentChildren =
MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.EXACTLY ||
MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.EXACTLY;
//父類可以會多次調用onMeasure,為了避免數據疊加,每次先清空數據
mMatchParentChildren.clear();
int maxHeight = 0;
int maxWidth = 0;
int childState = 0;
//測量所有子View,并記錄最大的寬高
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (mMeasureAllChildren || child.getVisibility() != GONE) {
//這里是設置子view的measureSpec,并調用子view的onmeasure方法,與getChildMeasureSpec()不同的是,這個measureChildMargin()方法把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());
//
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());
}
//主要處理viewGroup的at_most、unspecified兩種模式的大小,at_most:大小不能超過父viewGroup默認大小,unspecified默認是子view所需的大小
setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, childState),
resolveSizeAndState(maxHeight, heightMeasureSpec,
childState << MEASURED_HEIGHT_STATE_SHIFT));
//將所有子view的寬高的是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);
}
//給match_parent的子view重新設置大小
//這里又調用了子View的measure()方法,在上面的確定子view的measureSpec已經調用了一次,所以子view可能會被父view調用多次onMeasure(),要記得數據清楚。
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}
}
}
FrameLayout的match_parent的子view會被調用兩次onMeasure,所以再onMeasure()要注意數據的清楚,FrameLayout這里onMeasure也是每調用一次就會清除List的集合。大概實現思路是:先記錄子View的最大寬高->確定FrameLayout自己的大小->重新設置width、height是match_parent的大小。
FrameLayout的onLayout()源碼:
onLayout布局再FrameLayout的大小確定之后,因為其子View不用涉及到換行等問題,一般子view如果不設置gravity的屬性,默認是在FrameLayout左上顯示。所以這里的布局,只需要判斷gravity的屬性,然后根據屬性值來動態確認left和top的位置
//frameLayout所有的子View都是從容器左上方開始,除了一個gravity
@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;
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;
}
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);
}
}
}