在一次使用FrameLayout的时候,FrameLayout大小是自适应,其中一个子视图的宽高都是match_parent,也就是由FrameLayout的最终大小决定,结果出现了子视图大小取了默认值的情况,这就很是纳闷了
"match_parent"
android:layout_height="wrap_content">
<com.csw.fragmenttest.imageloopplayer.DotIndicate
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#AA000000" />
"wrap_content"
android:layout_height="60dp"
android:gravity="center"
android:text="测试文本" />
通过对自定义视图测量方法打断点,发现高度的模式是AT_MOST。。。
研究FrameLayout的源码,发现FrameLayout测量方法有点诡异。
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int count = getChildCount();
//如果FrameLayout宽高有不是精确值的,需要再测量一次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;
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());
//如果需要测量match_parent宽或高的子视图,将match_parent子视图添加到集合
if (measureMatchParentChildren) {
if (lp.width == LayoutParams.MATCH_PARENT ||
lp.height == LayoutParams.MATCH_PARENT) {
mMatchParentChildren.add(child);
}
}
}
}
// 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());
}
//完成测量
setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, childState),
resolveSizeAndState(maxHeight, heightMeasureSpec,
childState << MEASURED_HEIGHT_STATE_SHIFT));
//读取需要再测量的子视图数量
count = mMatchParentChildren.size();
//如果数量大于1???
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;
//如果是match_parent,测量子视图时使用精确模式+可用宽度
//下面的高度也是差不多
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);
}
}
}
如以上代码所展示,不知为何要match_parent属性的子视图多于一个才会再去测量一次,只有一个子视图时match_parent时子视图就可以无视吗。。。
解决方法:
1、多加一个match_parent的子视图,凑足>1。。。。
2、如上面的实例布局,修改一下另一个视图的布局参数,修改高度也为match_parent(也是凑足>1),然后设置最小高度,这样他在第一次AT_MOST的测量中就会计算出60dp的测量高度
"match_parent"
android:layout_height="wrap_content">
<com.csw.fragmenttest.imageloopplayer.DotIndicate
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#AA000000" />
"wrap_content"
android:layout_height="match_parent"
android:minHeight="60dp"
android:gravity="center"
android:text="测试文本" />