2月12日相对布局RelativeLayout

本文详细解读如何在Android布局中使用相对布局让红色背景在绿色背景下显现,涉及布局顺序、父容器定位、兄弟容器定位以及组件边距设置。关键技巧包括设置`layout_toLeftOf`属性和精确调整布局位置。
摘要由CSDN通过智能技术生成

1.相对布局是从界面的左上角开始布局,因此连续两个相对布局中后一个会掩盖前一个
例如,红色被绿色遮盖
在这里插入图片描述

<?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">

    <RelativeLayout
        android:background="#ff0000"
        android:layout_width="100dp"
        android:layout_height="100dp"/>

    <RelativeLayout
        android:background="#00ff00"
        android:layout_width="100dp"
        android:layout_height="100dp"/>

</RelativeLayout>

2.根据父容器定位(以最外层为基础来定位)
使红色显现出来
在这里插入图片描述

<?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">

    <RelativeLayout
        android:background="#ff0000"
        android:layout_alignParentRight="true"
        android:layout_width="100dp"
        android:layout_height="100dp"/>

    <RelativeLayout
        android:background="#00ff00"
        android:layout_width="100dp"
        android:layout_height="100dp"/>

</RelativeLayout>

3.根据兄弟容器定位
由于兄弟容器有很多,所以相对哪一个要设置id

给红色布局设置id

        android:id="@+id/rl1"

注意是@+id

绿色布局使用时是@id

        android:layout_toLeftOf="@id/rl1"

4.设置组件与父容器的边距
与左边相距100dp

        android:layout_marginLeft="100dp"

在这里插入图片描述

上下左右偏移100dp

        android:layout_margin="100dp"

在这里插入图片描述
5.设置组件内部元素的边距
在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="100dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout
        android:id="@+id/rl1"
        android:background="#ff0000"
        android:layout_centerInParent="true"
        android:layout_width="100dp"
        android:layout_height="100dp"/>

    <RelativeLayout
        android:background="#00ff00"
        android:layout_width="100dp"
        android:layout_height="100dp"/>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值