先看我原来的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