RelativeLayout的layout_marginBottom属性失效问题





  
  
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width= "wrap_content"
  4. android:layout_height= "wrap_content">
  5. <TextView
  6. android:id= "@+id/text_view1"
  7. android:layout_width= "wrap_content"
  8. android:layout_height= "wrap_content"
  9. android:layout_alignParentBottom= "true"
  10. android:layout_marginBottom= "50dp"
  11. android:text= "hello world1" />
  12. </RelativeLayout>


读完这个布局,通过脑补画面,你可能认为:一个TextView距底部50dp像素。

如果你真的这样认为,那么你就错了,上面的布局运行后的真实情况如图:


对,android:layout_marginBottom=”50dp”这句代码失效了,为什么呢?我也不知道,继续寻找规律


接下来把RelativeLayout设置layout_height=“match_parent”,


  
  
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width= "wrap_content"
  4. android:layout_height= "<span style=" color:# ff0000;">match_parent </span>">
  5. <TextView
  6. android:id= "@+id/text_view1"
  7. android:layout_width= "wrap_content"
  8. android:layout_height= "wrap_content"
  9. android:layout_alignParentBottom= "true"
  10. android:layout_marginBottom= "50dp"
  11. android:text= "hello world1" />
  12. </RelativeLayout>
看效果:



这时发现android:layout_marginBottom=”50dp”这句代码起作用了。



然后再继续研究,RelativeLayout android:layout_height=”wrap_content”的情况,在text_view1上面再增加一个TextView



  
  
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width= "wrap_content"
  4. android:layout_height= "<span style=" color:# ff0000;">wrap_content </span>">
  5. <TextView
  6. android:id= "@+id/text_view2"
  7. android:layout_width= "wrap_content"
  8. android:layout_height= "wrap_content"
  9. android:layout_above= "@+id/text_view1"
  10. android:layout_marginBottom= "50dp"
  11. android:text= "hello world2" />
  12. <TextView
  13. android:id= "@+id/text_view1"
  14. android:layout_width= "wrap_content"
  15. android:layout_height= "wrap_content"
  16. android:layout_alignParentBottom= "true"
  17. android:layout_marginBottom= "10dp"
  18. android:text= "hello world1" />
  19. </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属性才会有效

转载于:https://blog.csdn.net/w958796636/article/details/52921584



猜你喜欢

转载自blog.csdn.net/qq_41979349/article/details/82459318