Android 2.3上使用FrameLayout遇到的问题


问题一:
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;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值