android布局之RelativeLayout

RelativeLayout被称作相对布局,也是一种常见的布局。相比排列规律的线性布局,相对布局就显得自由一点

首先介绍一下一些常用的属性:
第一类:相对于父布局进行定位
android:layout_alignParentBottom=”true”下对齐
android:layout_alignParentTop=”true” 上对齐
android:layout_alignParentEnd=”true” 在父控件结束位置结束
android:layout_alignParentStart=”true” 在父控件开始位置开始
android:layout_alignParentRight=”true” 右对齐
android:layout_alignParentLeft=”true” 左对齐
android:layout_centerHorizontal=”true” 水平中线对齐
android:layout_centerVertical=”true” 垂直中线对齐
android:layout_centerInParent=”true” 居中

第二类:相对于控件进行定位
android:layout_alignBottom=”@id/**” 与控件下对齐
android:layout_alignTop=”@id/**” 与控件上对齐
android:layout_alignRight=”@id/**” 与控件右对齐
android:layout_alignLeft=”@id/**” 与控件左对齐
android:layout_below=”@id/**” 在控件下方
android:layout_above=”@id/**” 在控件上方
android:layout_toLeftOf=”@id/**” 在控件左侧
android:layout_toRightOf=”@id/**” 在控件右侧
android:layout_toStartOf=”@id/**” 一直到控件的开始位置
android:layout_toEndOf=”@id/**” 从控件的结束位置开始

先上代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/btn1"
        android:text="btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true" />
    <Button
        android:id="@+id/btn2"
        android:text="btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"/>
    <Button
        android:id="@+id/btn3"
        android:text="btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_centerInParent="true"/>
    <Button
        android:id="@+id/btn4"
        android:text="btn4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_above="@id/btn3"
        android:layout_toStartOf="@id/btn3" />
    <Button
        android:id="@+id/btn5"
        android:text="btn5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/btn3"
        android:layout_alignStart="@id/btn3" />
</RelativeLayout>

在看效果:
这里写图片描述

需要注意的:当一个控件去引用另一个控件的id时,该控件一定要定义在所要引用的控件的后面。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值