解决android:layout_marginBottom在RelativeLayout中无效的问题

先看我原来的XML文件:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ImageView 
        android:id="@+id/avatar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"/>
</RelativeLayout>

我是将这个RelativeLayout作为ListView当中每行的布局文件,原来代码是上面这样的,省略了ImageView右边的几个TextView,反正我的目标就是做成类似短信那种。


真机运行的时候,ImageView的左,上,右都空出了8dp,但是唯独底部直接就跟listview的分割线合在一起了,说明marginBottom属性没起作用。


原因是因为ImageView下方没有别的控件  (至于为什么下方没有控件然后marginBottom失效我也还不知道。。。希望知道的人能告诉我)


在stackoverflow上看到几个帖子,真正给我启发的是参考1,我按他原文做还是不行,所以直接改了下,看我改后代码


<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ImageView 
        android:id="@+id/avatar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"/>
   
    <View 
	android:layout_width="0px"
	android:layout_height="0px"
	android:layout_below="@id/avatar"/>
</RelativeLayout>


解决方案就是在下面加一个看不见的View,然后设置android:layout_below="@id/avatar"


------------------------------------------------------------------

android:layout_Bottom doesn't work in RelativeLayout:

add a View like following:

<View 
android:layout_width="0px"
android:layout_height="0px"
android:layout_below="@id/xxxxxr"/>


xxxxx is the id which you want fix

-------------------------------------------------------------------

参考:
1. http://stackoverflow.com/questions/4100272/android-layout-marginbottom-doesnt-seem-to-work-properly-in-2-2-froyo
2. http://www.eoeandroid.com/thread-12447-1-1.html

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值