问题一:Margin设置无效
原因:属于2.3的bug吧
解决方案:
如果是如下形式:
<FrameLayout>
<YourView></YourView>
</FrameLayout>
在你自己的(YourView)中设置layout_gravity属性,这样你相应的Margin才能生效。
问题二:FrameLayout在使用margin属性之后,并没有达到预期的效果,出现了图像被遮挡住了等现象。
当FrameLayout在计算大小的时候是如何进行的?
原因:下面放两段代码,就很容易理解了。
Android 2.3 的FrameLayout源码相应的测绘函数(onMeasure)关键部分如下:(http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.3.3_r1/android/widget/FrameLayout.java#FrameLayout.onMeasure%28int%2Cint%29)
// Find rightmost and bottommost child
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (mMeasureAllChildren || child.getVisibility() != GONE) {
measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);
maxWidth = Math.max(maxWidth, child.getMeasuredWidth());
maxHeight = Math.max(maxHeight, child.getMeasuredHeight());
}
}
Android 5.1 的FrameLayout源码相应的测绘函数(onMeasure)关键部分如下:(http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.0_r1/android/widget/FrameLayout.java#FrameLayout)
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);
}
}
}
}
显而易见,2.3系统上的FrameLayout在计算大小的时候,把childView的margin属性直接忽略了,忽略了。
解决方案:
1、要么使用其他layout进行代替
2、在代码中进行相应的设置,也即在代码中修改FrameLayout的属性
如,在Y轴我想进行相应的操作,
mFrameLayoutView.getLayoutParams().height = mFrameLayoutView.getHeight()
((ViewGroup.MarginLayoutParams) mInnerView.getLayoutParams()).bottomMargin;