相对布局RelativeLayout的子控件属性和操作

RelativeLayout是相对布局控件,它包含的子控件将以控件之间的相对位置或者子类控件相对父类容器的位置方式排列。

子类控件常用属性

android:layout_alignParentLeft
-子类控件相对当前父类容器靠左

代码:

    <Button 
        android:id="@+id/bt1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="One"
        android:layout_alignParentLeft="true"/>

显示效果:

这里写图片描述

android:layout_alignParentTop
-子类控件相对当前父类容器靠上

代码:

    <Button 
        android:id="@+id/bt1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="One"
        android:layout_alignParentTop="true"/>

显示效果:

这里写图片描述

android:layout_marginLeft
-子类控件距离父类容器左边的距离

代码:

    <Button 
        android:id="@+id/bt1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="One"
        android:layout_marginLeft="40dp"/>

显示效果:

这里写图片描述
android:layout_marginTop

-子类控件距离父类容器上边的距离

代码:

    <Button 
        android:id="@+id/bt1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="One"
        android:layout_marginTop="40dp"/>

显示效果:

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
是的,相对布局的标签是`RelativeLayout`。 `RelativeLayout`是一种常用的布局方式,它允许我们将控件相对于其他控件或父布局进行定位。我们可以通过设置控件之间的相对位置关系,来实现不同的布局效果。 例如,以下代码展示了如何使用`RelativeLayout`来实现一个简单的布局: ``` <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello, World!" android:layout_centerInParent="true" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click Me!" android:layout_below="@id/textView1" android:layout_centerHorizontal="true" /> </RelativeLayout> ``` 在代码中,`RelativeLayout`标签用于定义一个相对布局,`TextView`和`Button`控件分别设置了不同的相对位置关系。例如,`TextView`控件使用了`layout_centerInParent`属性,表示该控件应该居中于父布局中。而`Button`控件则使用了`layout_below`和`layout_centerHorizontal`属性,表示该控件应该位于`TextView`控件的下方,并且水平居中于父布局中。 需要注意的是,`RelativeLayout`布局方式比较灵活,可以根据实际需求设置不同的相对位置关系。同时,由于需要计算控件之间的相对位置关系,因此相对布局可能会比其他布局方式更加耗费资源。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值