<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text_view1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="50dp"
android:text="hello world1" />
</RelativeLayout>
读完这个布局,通过脑补画面,你可能认为:一个TextView距底部50dp像素。
如果你真的这样认为,那么你就错了,上面的布局运行后的真实情况如图:
对,android:layout_marginBottom="50dp"这句代码失效了,为什么呢?我也不知道,继续寻找规律
接下来把RelativeLayout设置layout_height=“match_parent”,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="<span style="color:#ff0000;">match_parent</span>">
<TextView
android:id="@+id/text_view1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="50dp"
android:text="hello world1" />
</RelativeLayout>
看效果:
这时发现android:layout_marginBottom="50dp"这句代码起作用了。
然后再继续研究,RelativeLayout android:layout_height="wrap_content"的情况,在text_view1上面再增加一个TextView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="<span style="color:#ff0000;">wrap_content</span>">
<TextView
android:id="@+id/text_view2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/text_view1"
android:layout_marginBottom="50dp"
android:text="hello world2" />
<TextView
android:id="@+id/text_view1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:text="hello world1" />
</RelativeLayout>
这时发现新增加的TextView的android:layout_marginBottom="50dp"起作用了。
最后总结:
RelativeLayout布局里
1、当设置为android:layout_height="wrap_content"时,最下面的控件layout_marginBottom属性无效,如果其他控件使用layout_above让自己处于最下面的控件之上,那么layout_marginBottom属性有效。
2、当设置为android:layout_height="match_parent"时,或者高度为固定值,那么最下面的控件layout_marginBottom属性才会有效。