关于layout_alignParentLeft、layout_alignLeft、layout_toLeftOf、layout_marginLeft的区别

关于layout_alignParentLeft、layout_alignLeft、layout_toLeftOf、layout_marginLeft的区别

1.layout_alignParentLeft

文档解释:

If true, makes the left edge of this view match the left edge of the parent. (贴紧父元素的左边缘 )

属性值:true或者false
使用样例:

<!--注意:layout_alignParentLeft要和layout_alignParentStart一起搭配使用-->

<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:text="@string/按钮"
        android:textSize="16sp"
        app:backgroundTint="#A1FF9800" />

样例结果:
在这里插入图片描述

2.layout_alignLeft

文档解释

Makes the left edge of this view match the left edge of the given anchor view ID.(本元素的左边缘和某元素的的左边缘对齐 )

属性值:必须为id的引用名“@id/id-name”
使用样例:

<!--注意:layout_alignLeft要和layout_alignStart一起搭配使用-->
<Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@id/button1"
        android:layout_alignLeft="@id/button1"
        android:layout_below="@id/button1"
        android:text="@string/还是按钮"
        android:textSize="16sp"
        app:backgroundTint="#BA3F51B5" />

样例结果:

注意
如果此处的button2缺少了android:layout_below="@id/button1"这句,那么就会出现button2叠加在button1上面,你看不到“按钮”的这个按钮的情况。这是因为layout_alignLeft的功能就是使得button2的左边缘和button1的的左边缘对齐的缘故。

3.layout_toLeftOf

文档解释

Positions the right edge of this view to the left of the given anchor view ID.(在某元素的左边 )

属性值:必须为id的引用名“@id/id-name”
使用样例:

<!--注意:layout_toLeftOf和layout_toStartOf一起搭配使用-->
<!--此处为了更直观感受,就使用layout_centerInParent="true"把button1安排到界面居中位置了-->
<Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_toLeftOf="@id/button1"
        android:layout_toStartOf="@id/button1"
        android:text="@string/又是按钮"
        android:textSize="16sp"
        app:backgroundTint="#8B009688"
         />

样例结果:
在这里插入图片描述

4.layout_marginLeft

文档解释:

(离某元素左边缘的距离 )

属性值:具体的像素值,如30sp等
使用样例:

<!--注意:layout_marginLeft与layout_marginStart一起搭配使用-->
<Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/button1"
        android:layout_marginLeft="100sp"
        android:layout_marginStart="100sp"
        android:text="@string/又双叒是按钮"
        android:textSize="16sp"
        app:backgroundTint="#8BF44336"
        />

样例结果:
在这里插入图片描述

5.总结

XML attributesvalueUsed together
layout_alignParentLefttrue或falselayout_alignParentStart
layout_alignLeft@id/id_namelayout_alignStart
layout_toLeftOf@id/id_namelayout_toStartOf
layout_marginLeft具体的像素值layout_marginStart

如果想要了解更多,可以点击看看它: Layout常用属性介绍。(一个大佬的文章)

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Java中,RelativeLayout是一种布局管理器,用于在屏幕上放置和定位视图。layout_toLeftOf是RelativeLayout的一个属性,用于指定一个视图相对于另一个视图的左侧位置。 如果在使用layout_toLeftOf属性时无法显示视图,可能有以下几个原因: 1. 没有正确设置视图的ID:在RelativeLayout中,每个视图都需要有一个唯一的ID,以便其他视图可以引用它。请确保你为每个视图设置了唯一的ID,并在layout_toLeftOf属性中正确引用了目标视图的ID。 2. 没有正确设置视图的宽度和高度:RelativeLayout中的视图需要设置宽度和高度,以便其他视图可以相对定位。如果视图的宽度或高度设置为wrap_content或match_parent,可能会导致布局问题。请确保你为视图设置了具体的宽度和高度。 3. 没有正确设置视图的位置规则:除了layout_toLeftOf属性,RelativeLayout还有其他一些属性,如layout_alignParentLeftlayout_alignLeft等,用于指定视图的位置规则。请确保你正确设置了视图的位置规则,以便layout_toLeftOf属性可以生效。 以下是一个示例代码,演示了如何在RelativeLayout中使用layout_toLeftOf属性: ```java RelativeLayout layout = new RelativeLayout(context); // 创建两个视图 TextView textView1 = new TextView(context); textView1.setId(View.generateViewId()); textView1.setText("TextView 1"); TextView textView2 = new TextView(context); textView2.setId(View.generateViewId()); textView2.setText("TextView 2"); // 设置视图的宽度和高度 RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT ); textView1.setLayoutParams(params1); RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT ); textView2.setLayoutParams(params2); // 设置视图的位置规则 params2.addRule(RelativeLayout.LEFT_OF, textView1.getId()); // 将视图添加到RelativeLayoutlayout.addView(textView1); layout.addView(textView2); // 将RelativeLayout设置为Activity的内容视图 setContentView(layout); ``` 请注意,以上代码仅为示例,实际使用时需要根据具体情况进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值