Android Studio相对布局(RelativeLayout)最基础知识点

RelativeLayout布局基础知识详解

每个属性只举1-2个例子哦,操作都是差不多的,如果还有问题的话,欢迎留言😄
1.基本属性
gravity用于设置容器内组件的对齐方式

  • center中间
  • left左边
  • right右边
  • top上边
  • bottom下边
    话不多说,上图,上代码!!!
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#BBFFFF"
    android:gravity="center">
    <View
        android:id="@+id/view_1"
        android:layout_height="100dp"
        android:layout_width="100dp"
        android:background="#FFF5EE"
        >
    </View>

</RelativeLayout>

在这里插入图片描述
ignoreGravity若组件这一属性设置为true,那么这一组件将不会受到gravity属性的影响
2.根据父容器定位
layout_alignParentLeft和父容器左对齐
话不多说,上图,上代码!!!

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#BBFFFF">
    <View
        android:id="@+id/view_1"
        android:layout_height="100dp"
        android:layout_width="100dp"
        android:background="#000000"
        android:layout_alignParentLeft="true"
        >
    </View>
</RelativeLayout>

在这里插入图片描述
layout_alignParentRight和父容器右对齐
layout_alignParentTop和父容器顶部对齐
layout_alignParentBottom和父容器底部对齐
layout_centerHorizontal水平居中
layout_centerVertical垂直居中
layout_centerInParent中间位置
话不多说,上图,上代码!!!

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#BBFFFF">

    <View
        android:id="@+id/view_1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerInParent="true"
        android:background="#FFF5EE">
    </View>
</RelativeLayout>

在这里插入图片描述

3.根据兄弟组件定位
layout_toLeftOf兄弟组件的左边
话不多说,上图,上代码!!!

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#BBFFFF">

    <View
        android:id="@+id/view_1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#FFF5EE">

    </View>

    <View
        android:id="@+id/view_2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_toRightOf="@+id/view_1"
        android:background="#7FFFD4">

    </View>

</RelativeLayout>

在这里插入图片描述

layout_toRightOf兄弟组件的右边
layout_above兄弟组件的上面
layout_below兄弟组件的下面
layout_alignTop对齐兄弟组件的上边界
layout_alignBottom对齐兄弟组件的下边界
layout_alignRight对齐兄弟组件的右边界
layout_alignLeft对齐兄弟组件的左边界
4.外间距(margin)
layout_margin设置组件上下左右的间距
话不多说,上图,上代码!!!

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="30dp"
    android:background="#BBFFFF">

    <View
        android:id="@+id/view_1"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_centerInParent="true"
        android:background="#000000">
        
    </View>

    <View
        android:id="@+id/view_2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#7FFFD4">

    </View>

</RelativeLayout>

在这里插入图片描述
layout_marginLeft设置组件离左边的间距
layout_marginRight设置组件离右边的间距
layout_marginTop设置组件离上方的间距
layout_marginBottom设置组件离下方的间距
5.内边距(padding)
padding内部组件的上下左右填充一定边距
话不多说,上图,上代码!!!

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="30dp"
    android:background="#BBFFFF"
    android:paddingTop="30dp">

    <View
        android:id="@+id/view_1"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="#000000">

    </View>

    <View
        android:id="@+id/view_2"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_below="@+id/view_1"
        android:background="#7FFFD4">

    </View>

</RelativeLayout>

在这里插入图片描述

paddingLeft内部组件的左边填充一定的边距
paddingRight内部组件右边填充一定的边距
paddingTop内部组件上方填充一定的边距
paddingBottom内部组件下方填充一定的边距
最后的最后再来个嵌套的供大家加深理解!!!

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="30dp"
    android:background="#BBFFFF">

    <View
        android:id="@+id/view_1"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="#000000">

    </View>

    <View
        android:id="@+id/view_2"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_below="@+id/view_1"
        android:background="#7FFFD4" />

    <RelativeLayout
        android:id="@+id/view_3"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_below="@+id/view_2"
        android:background="#9370DB"
        android:padding="15dp">

        <View
            android:id="@+id/view_4"
            android:layout_width="150dp"
            android:layout_height="match_parent"
            android:background="#CD7054">

        </View>

        <View
            android:layout_width="150dp"
            android:layout_height="match_parent"
            android:background="#CD7054"
            android:layout_toRightOf="@+id/view_4"
            android:layout_marginLeft="20dp">

        </View>
    </RelativeLayout>


</RelativeLayout>

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值