[原创]在Framelayout中放置button控件出现的覆盖问题

android Framelayout(帧布局)是很常用的布局,主要用来处理需要多个view叠加显示的情况。

然而在使用中,我发现Framelayout中的Button控件,会挡住所有其他控件,而不论位置和添加顺序如何,这个表现是不正常的,我本机有4.1和5.0两种模拟器,只有5.0有这个问题,因此怀疑是5.0的bug。

下面用一个例子说明:

想要的效果是这样的:  

我选择使用Framelayout来实现,下面一个button,上面一个imageview即可,代码如下:

 1     <FrameLayout
 2         android:layout_width="match_parent"
 3         android:layout_height="wrap_content">
 4 
 5 
 6             <Button
 7                 android:layout_width="match_parent"
 8                 android:layout_height="wrap_content"
 9                 android:text="立即投资" />
10 
11         <ImageView
12             android:layout_width="36dp"
13             android:layout_height="36dp"
14             android:layout_gravity="left|center"
15             android:layout_marginLeft="16dp"
16             android:src="@mipmap/ic_invest_button"
17             android:tint="@color/white" />
18 
19     </FrameLayout>

在4.1系统上,工作良好,换到5.0,左边的小图标(imageview)死活不显示,调试了半天才发现是被button的背景遮住了。

imageview是后加入的,按理来说,应该是显示在button上面,而不应该被挡住,所以觉得这应该是5.0的一个bug。

但是咱们也必须得兼容5.0,不能显示异常啊,那咱们自己来解决这个问题吧,因为本项目其他地方也用到了Framelayout,没有出现异常,只有这里异常,对比测试后发现,是button控件的问题,那假如我就用一个容器把button包裹起来,结果会怎么样呢?直接上代码:

 1     <FrameLayout
 2         android:layout_width="match_parent"
 3         android:layout_height="wrap_content">
 4 
 5         <RelativeLayout
 6             android:layout_width="match_parent"
 7             android:layout_height="wrap_content">
 8 
 9             <Button
10                 android:layout_width="match_parent"
11                 android:layout_height="wrap_content"
12                 android:text="立即投资" />
13 
14         </RelativeLayout>
15 
16         <ImageView
17             android:layout_width="36dp"
18             android:layout_height="36dp"
19             android:layout_gravity="left|center"
20             android:layout_marginLeft="16dp"
21             android:src="@mipmap/ic_invest_button"
22             android:tint="@color/white" />
23 
24     </FrameLayout>

跑起来一看,问题解决了,完美兼容4.0,5.0。只是多了一层嵌套,代码上看略微不优雅。

 

posted @ 2015-07-17 16:28  sky0014  阅读(10526)  评论(0编辑  收藏  举报