Android布局———RelativeLayout布局(相对布局)

RelativeLayout布局方式是一种让组件以相对于容器或者相对于容器的另一个组件的相对位置进行放置的布局方式。RelativeLayout布局的继承关系如下:

java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.RelativeLayout
      id.widget.RelativeLayout
通过RelativeLayout布局可以设置控件与父控件的相对位置或控件间的相对位置。RelativeLayout布局的常用属性有:

相对于其他子控件:

android:layout_above              将该组件的底部置于给定id的组件之上

android:layout_below              将该组件的底部置于给定id的组件之下

android:layout_toLeftOf         将该组件的右边缘与给定id 的组件左边缘对齐

android:layout_toRightOf         将该组件的左边缘与给定id 的组件右边缘对齐

android:layout_alignBaseline       将该组件的baseline(基准线)与给定id的baseline对齐

android:layout_alignBottom        将该组件的底边与给定id的组件底边对齐

android:layout_alignTop       将该组件的顶部边缘与给定id的组件顶部边缘对齐

android:layout_alignLeft       将该组件的左边缘与给定id的组件左边缘对齐

android:layout_alignRight     将该组件的右边缘与给定id的组件右边缘对齐

相对于父控件:

android:layout_alignParentTop    为true, 将该组件的顶部与其父组件的顶部对齐

android:layout_alignParentBottom    为true,将该组件的低部与其父组件的低部对齐

android:layout_alignParentLeft    为true, 将该组件的左边与其父组件的左边对齐

android:layout_alignParentRight   为true,将该组件的右边与其父组件的右边对齐

android:layout_centerHorizontal   为true,将该组件置于父组件水平居中

android:layout_centerVertical  为true,将该组件置于父组件垂直居中

android:layout_centerInParent 为true,将该组件置于父组件水平垂直居中


需要注意的是:RelativeLayout布局不支持anroid:layout_weight属性,当没有指明和其他控件之间的位置关系时,控件位于父控件的左上方。

RelativeLayout布局例子如下:

main.xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.alarmmanagerdemo.MainActivity" >


    


    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="35dp"
        android:text="Button1" />


    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="22dp"
        android:text="Button2" />


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


    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button3"
        android:layout_below="@+id/button3"
        android:layout_marginTop="30dp"
        android:text="Button4" />
</RelativeLayout>

运行效果:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值