一个demo学习完RelativeLayout

关于相对布局:
1.组件默认位置都是在左上角,组件之间可以重叠
2.组件可以相对父元素上下左右对齐,相对父元素水平居中、竖直居中、水平竖直同时居中
3.组件可以位于其他组件的上下左右对齐
4.组件可以布局于其他组件的上方,下方,左方,右方。

下面是一个简单的demo,使用了上面除重叠以外的所有功能,效果图如下:
这里写图片描述

简单明了直接上代码:

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

    <Button
        android:id="@+id/btn_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="中间(基准点)" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/btn_center"
        android:layout_alignParentLeft="true"
        android:layout_alignTop="@+id/btn_center"
        android:layout_toLeftOf="@+id/btn_center"
        android:text="左边" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/btn_center"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/btn_center"
        android:layout_toRightOf="@+id/btn_center"
        android:text="右边" />


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btn_center"
        android:layout_alignLeft="@+id/btn_center"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/btn_center"
        android:text="上边" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btn_center"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/btn_center"
        android:layout_below="@+id/btn_center"
        android:text="下边" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btn_center"
        android:layout_toLeftOf="@+id/btn_center"
        android:text="左上角" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btn_center"
        android:layout_toRightOf="@+id/btn_center"
        android:text="右上角" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn_center"
        android:layout_toLeftOf="@+id/btn_center"
        android:text="左下角" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn_center"
        android:layout_toRightOf="@+id/btn_center"
        android:text="右下角" />
</RelativeLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值