四种基本布局之二——RelativeLayout

RelativeLayout相对布局

于LinearLayout相比,RelativeLayout 更加随意,可以通过相对定位的方式让控件出现在布局的任何位置,
正因为如此,RelativeLayout的属相非常多

<RelativeLayout  xmlns: android = "http://schemas.android.com/apk/res/android"
    android :layout_width= "match_parent"
    android :layout_height= "match_parent" >

    <Button
        android :id= "@+id/button1"
        android :layout_width= "wrap_content"
        android :layout_height= "wrap_content"
        android :layout_alignParentTop= "true"
        android :layout_alignParentLeft= "true"
        android :text= "Button1" />

    <Button
        android :id= "@+id/button2"
        android :layout_width= "wrap_content"
        android :layout_height= "wrap_content"
        android :layout_alignParentRight= "true"
        android :layout_alignParentTop= "true"
        android :text= "Button2" />

    <Button
        android :id= "@+id/button3"
        android :layout_width= "wrap_content"
        android :layout_height= "wrap_content"
        android :layout_centerInParent= "true"
        android :text= "Button3" />

    <Button
        android :id= "@+id/button4"
        android :layout_width= "wrap_content"
        android :layout_height= "wrap_content"
        android :layout_alignParentBottom= "true"
        android :layout_alignParentLeft= "true"
        android :text= "Button4" />

    <Button
        android :id= "@+id/button5"
        android :layout_width= "wrap_content"
        android :layout_height= "wrap_content"
        android :layout_alignParentBottom= "true"
        android :layout_alignParentRight= "true"
        android :text= "Button5" />

</RelativeLayout>

//相对于父布局进行定位
//Button1于父布局的左上角对齐
//Button2于父布局的右上角对齐
//Button3于父布局的居中显示
//Button4于父布局的左下角对齐
//Button5于父布局的右下角对齐



也可以控件相对于控件进行定位

<RelativeLayout  xmlns: android = "http://schemas.android.com/apk/res/android"
    android :layout_width= "match_parent"
    android :layout_height= "match_parent" >

    <Button
        android :id= "@+id/button3"
        android :layout_width= "wrap_content"
        android :layout_height= "wrap_content"
        android :layout_centerInParent= "true"
        android :text= "Button3" />
   
    <Button
        android :id= "@+id/button1"
        android :layout_width= "wrap_content"
        android :layout_height= "wrap_content"
        android :layout_above= "@+id/button3"
        android :layout_toLeftOf= "@+id/button3"
        android :text= "Button1" />

    <Button
        android :id= "@+id/button2"
        android :layout_width= "wrap_content"
        android :layout_height= "wrap_content"
        android :layout_above= "@+id/button3"
        android :layout_toRightOf= "@+id/button3"
        android :text= "Button2" />
   
    <Button
        android :id= "@+id/button4"
        android :layout_width= "wrap_content"
        android :layout_height= "wrap_content"
        android :layout_below= "@+id/button3"
        android :layout_toLeftOf= "@+id/button3"
        android :text= "Button4" />

    <Button
        android :id= "@+id/button5"
        android :layout_width= "wrap_content"
        android :layout_height= "wrap_content"
        android :layout_below= "@+id/button3"
        android :layout_toRightOf= "@+id/button3"
        android :text= "Button5" />

</RelativeLayout>

//注意:当一个控件去引用另一个控件的id时,该控件一定要定义在引用控件的后面,不然会出现找不到id的情况


RelativeLayout 还有另外一组相对于控件进行定位的属性
//android:layout_alignLeft 表示让一个控件的左边缘和另一个控件的左边缘对齐
//android:layout_alignRight 表示让一个控件的右边缘和另一个控件的右边缘对齐
//android:layout_alignTop 表示让一个控件的上边缘和另一个控件的上边缘对齐
//android:layout_alignButtom 表示让一个控件的下边缘和另一个控件的下边缘对齐











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值