Android——RelativeLayout(相对布局)

本文详细介绍了Android应用开发中常用的RelativeLayout布局属性及其使用方法。通过实例展示了如何实现组件在父容器内的定位,包括居中显示、对齐边缘等效果,并且讲解了组件间相对位置的设置技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、相对于父容器

1.居中

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

    <!--相对于父容器
    1.居中
    2.同方向对齐方式
    -->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮一"
        android:layout_centerInParent="true"
        /><!--位于父容器居中位置-->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮二"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        /><!--与父容器底端对齐-->
          <!--位于父容器水平居中位置-->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮三"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        /><!--与父容器右侧对齐-->
          <!--位于父容器垂直居中位置-->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮四"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        /><!--与父容器左侧对齐-->
    <!--位于父容器垂直居中位置-->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮五"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        /><!--与父容器顶端对齐-->
    <!--位于父容器水平居中位置-->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮六"
        /><!--与父容器左侧对齐-->
    <!--与父容器顶端对齐-->
    <!--默认位置-->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮七"
        android:layout_alignParentRight="true"
        /><!--与父容器右侧对齐-->
    <!--与父容器顶端对齐-->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮八"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        /><!--与父容器左侧对齐-->
    <!--与父容器底端对齐-->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮十"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
      /><!--与父容器右侧对齐-->
    <!--与父容器底端对齐-->
</RelativeLayout>

二、与兄弟组件的相对位置

1.同方向

2.反方向

+lay_outmargin +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">
    <!--与兄弟组件之间
    1.同方向
    2.反方向-->

    <!--margin  外边距
    padding   内间距
    -->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮一"
        android:layout_centerInParent="true"
        android:id="@+id/bt"/><!--父容器内居中-->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮二"
        android:layout_alignTop="@id/bt"
        android:layout_toLeftOf="@id/bt"/>
    //相对于按钮一  顶部对齐  位于左侧

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮三"
        android:layout_alignRight="@id/bt"
        android:layout_above="@id/bt"/>
    //相对于按钮一  右侧对齐  位于上方
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮四"
        android:layout_alignRight="@id/bt"
        android:layout_below="@id/bt"/>
    //相对于按钮一  右侧对齐  位于下方
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮五"
        android:layout_alignTop="@id/bt"
        android:layout_toRightOf="@id/bt"/>
    //相对于按钮一  顶部对齐  位于右侧
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮六"
        android:layout_toRightOf="@id/bt"
        android:layout_below="@id/bt"/>
    //相对于按钮一  位于右侧  位于下方
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮七"
        android:layout_toRightOf="@id/bt"
        android:layout_above="@id/bt"/>
    //相对于按钮一  位于右侧  位于上方

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮八"
        android:layout_toLeftOf="@id/bt"
        android:layout_above="@id/bt"/>
    //相对于按钮一  位于左侧  位于上方

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮九"
        android:layout_toLeftOf="@id/bt"
        android:layout_below="@id/bt"/>
    //相对于按钮一  位于左侧  位于下方
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="输入框"
        android:id="@+id/et"
        android:paddingLeft="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="10dp"/>
    //内左侧间距20dp 上边距20dp 下边距10dp

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="OK"
        android:layout_alignParentRight="true"
        android:layout_below="@id/et"
        android:id="@+id/ok"/>
    //ok按钮  位于父窗口右侧  输入框下方
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CANCLE"
        android:layout_alignTop="@id/ok"
        android:layout_toLeftOf="@id/ok"
        android:layout_marginRight="20dp"/>
    //cancle按钮  相对于ok按钮顶部对齐 位于左侧 右边距20dp





</RelativeLayout>

 

转载于:https://www.cnblogs.com/Chenshuai7/p/5321696.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值