RelativeLayout的layout_marginBottom属性失效问题

<?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属性才会有效

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在Android中,可以使用代码来控制布局中的`layout_marginBottom`属性。具体方法如下: 1. 获取对应的View对象。例如,如果要设置某个TextView的`layout_marginBottom`属性,首先需要获取该TextView的对象。 ```java TextView textView = findViewById(R.id.text_view); ``` 2. 创建`LayoutParams`对象。`LayoutParams`是布局参数的基类,其子类包括`LinearLayout.LayoutParams`、`RelativeLayout.LayoutParams`等。通过设置`LayoutParams`对象的`bottomMargin`属性来控制`layout_marginBottom`属性的值。 ```java LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) textView.getLayoutParams(); layoutParams.bottomMargin = 20; // 设置layout_marginBottom属性为20dp ``` 3. 将修改后的`LayoutParams`对象应用到View中。 ```java textView.setLayoutParams(layoutParams); ``` 完整的示例代码如下: ```java TextView textView = findViewById(R.id.text_view); LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) textView.getLayoutParams(); layoutParams.bottomMargin = 20; // 设置layout_marginBottom属性为20dp textView.setLayoutParams(layoutParams); ``` 注意:在设置`LayoutParams`属性时,需要根据View所在的父容器的类型来选择对应的`LayoutParams`子类。例如,如果一个View是放在LinearLayout中的,就需要使用`LinearLayout.LayoutParams`来设置布局参数。如果不确定父容器的类型,可以通过`View.getParent()`方法获取父容器对象,再根据父容器对象的类型来选择相应的`LayoutParams`子类。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值