基础布局之RelativeLayout相对布局


概述

相对布局(RelativeLayout)是一种根据父容器和兄弟控件作为参照来确定控件位置的布局方式。
使用相对布局,需要将布局节点改成RelativeLayout,基本格式如下:

<?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:layout_alignParentTop控件顶部与父布局的顶部对齐。
android:layout_alignParentBottom控件底部与父布局的底部对齐。
android:layout_alignParentLeft控件左边与父布局的左边对齐。
android:layout_alignParentRight控件右边与父布局的右边对齐。
android:layout_alignParentStart将控件的起始边(根据布局方向,可能是左边或右边)与父容器的起始边对齐。
android:layout_alignParentEnd将控件的结束边(根据布局方向,可能是右边或左边)与父容器的结束边对齐。
android:layout_centerHorizontal将控件水平居中对齐。
android:layout_centerVertical将控件垂直居中对齐。
android:layout_centerInParent水平垂直都居中

相对定位属性:

属性作用
android:layout_above控件放置在指定控件的上方。
android:layout_below控件放置在指定控件的下方。
android:layout_toLeftOf控件放置在指定控件的左边。
android:layout_toRightOf控件放置在指定控件的右边。
android:layout_alignTop控件的顶部与指定控件的顶部对齐。
android:layout_alignBottom控件的底部与指定控件的底部对齐。
android:layout_alignLeft控件的左边与指定控件的左边对齐。
android:layout_alignRight控件的右边与指定控件的右边对齐。
android:layout_alignStart将控件的起始边(根据布局方向,可能是左边或右边)与指定控件的起始边对齐。
android:layout_alignEnd将控件的结束边(根据布局方向,可能是右边或左边)与指定控件的结束边对齐。
android:layout_alignBaseline控件的基线与指定控件的基线对齐。

其中start、end相关的两个属性通常用于支持不同语言的布局需求,例如从右到左的阿拉伯语布局。在安卓开发中,可以通过设置这两个属性来实现适应不同语言布局方向的界面设计。


二、父容器定位属性

2.1 示例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">
    
    <Button
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:background="@color/green" />

</RelativeLayout>

与父布局右边对齐,垂直居中
在这里插入图片描述

2.2 示例2

看一下 android:layout_centerInParent=“true”
在父布局中垂直、水平方向都居中。

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

    <Button
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_centerInParent="true"
        android:background="@color/green" />

</RelativeLayout>

在这里插入图片描述


三、相对定位属性

3.1 示例1

看一下
android:layout_above 控件放置在指定控件的上方。
android:layout_below 控件放置在指定控件的下方。
android:layout_toLeftOf 控件放置在指定控件的左边。
android:layout_toRightOf 控件放置在指定控件的右边。

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

    <Button
        android:id="@+id/center"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_centerInParent="true"
        android:background="@color/green" />

    <Button
        android:id="@+id/left"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_toLeftOf="@+id/center"
        android:background="@color/pink" />

    <Button
        android:id="@+id/right"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_toRightOf="@+id/center"
        android:background="@color/blue" />

    <Button
        android:id="@+id/top"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_above="@+id/center"
        android:background="@color/red" />

    <Button
        android:id="@+id/below"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_below="@+id/center"
        android:background="@color/yellow" />
</RelativeLayout>

在这里插入图片描述

3.2 示例2

在上面的基础上添加
android:layout_alignTop 控件的顶部与指定控件的顶部对齐。
android:layout_alignBottom 控件的底部与指定控件的底部对齐。
android:layout_alignLeft 控件的左边与指定控件的左边对齐。
android:layout_alignRight 控件的右边与指定控件的右边对齐。

分别与中心控件的顶部、底部、左边、右边对齐。

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

    <Button
        android:id="@+id/center"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_centerInParent="true"
        android:background="@color/green" />

    <Button
        android:id="@+id/left"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignTop="@+id/center"
        android:layout_toLeftOf="@+id/center"
        android:background="@color/pink" />

    <Button
        android:id="@+id/right"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignBottom="@id/center"
        android:layout_toRightOf="@+id/center"
        android:background="@color/blue" />

    <Button
        android:id="@+id/top"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_above="@+id/center"
        android:layout_alignLeft="@+id/center"
        android:background="@color/red" />

    <Button
        android:id="@+id/below"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_below="@+id/center"
        android:layout_alignRight="@+id/center"
        android:background="@color/yellow" />
</RelativeLayout>

在这里插入图片描述

3.3 示例3

看一下android:layout_alignBaseline的使用:

在安卓相对布局中,android:layout_alignBaseline 是用于将控件的基线与指定控件的基线对齐的属性。基线是文本行中字符底部对齐的虚拟线,通常用于对齐文本或文字相关的控件。

使用详解如下:

当一个控件设置了 android:layout_alignBaseline=“@id/otherView”,该控件的基线会与指定控件(@id/otherView)的基线对齐。
如果两个控件都设置了 android:layout_alignBaseline,则它们的基线会对齐。
如果指定的控件没有基线(比如非文本控件),则基线对齐的效果可能不明显。

没有添加layout_alignBaseline的效果:

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

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/yellow"
        android:text="hello" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/text1"
        android:background="@color/pink"
        android:padding="20dp"
        android:text="world" />
</RelativeLayout>

在这里插入图片描述
添加layout_alignBaseline的效果:
给textview1添加

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

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/yellow"
        android:layout_alignBaseline="@+id/text2"
        android:text="hello" />

    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/text1"
        android:background="@color/pink"
        android:padding="20dp"
        android:text="world"
        />
</RelativeLayout>

在这里插入图片描述

给textview2添加

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

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/yellow"
        android:text="hello" />

    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/text1"
        android:background="@color/pink"
        android:padding="20dp"
        android:text="world"
        android:layout_alignBaseline="@+id/text1"
        />
</RelativeLayout>

在这里插入图片描述


  • 27
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
相对布局RelativeLayout)是 Android 中常用的一种布局方式,可以根据控件之间的相对位置来确定它们的布局关系。以下是使用相对布局的步骤: 1. 在 XML 布局文件中,使用 `<RelativeLayout>` 标签将布局内容包裹起来。 2. 在 `<RelativeLayout>` 标签内部,使用 `<View>` 或其他布局控件标签来定义需要布局的控件。 3. 对于每个需要布局的控件,可以使用一些属性指定它们与其他控件的相对位置。常用属性包括: - `android:layout_alignParentTop`、`android:layout_alignParentBottom`、`android:layout_alignParentLeft`、 `android:layout_alignParentRight`:将控件与父容器的顶部、底部、左侧或右侧对齐。 - `android:layout_alignTop`、`android:layout_alignBottom`、`android:layout_alignLeft`、`android:layout_alignRight`: 将控件与指定控件的顶部、底部、左侧或右侧对齐。 - `android:layout_below`、`android:layout_above`、`android:layout_toLeftOf`、`android:layout_toRightOf`: 将控件放置在指定控件的下方、上方、左侧或右侧。 - `android:layout_centerInParent`:将控件居中于父容器。 - `android:layout_centerHorizontal`、`android:layout_centerVertical`:将控件水平或垂直居中于父容器。 这些属性可以与其他布局属性(如 `android:layout_width`、`android:layout_height`)结合使用,以确定控件的大小和位置。 4. 使用这些属性为每个控件设置相应的相对位置关系,调整它们的顺序和布局方式,以满足设计需求。 下面是一个示例: ```xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 1" android:layout_alignParentTop="true" android:layout_alignParentLeft="true"/> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 2" android:layout_below="@id/button1" android:layout_alignParentRight="true"/> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 3" android:layout_toLeftOf="@id/button2" android:layout_below="@id/button1" android:layout_marginRight="10dp"/> </RelativeLayout> ``` 在这个示例中,有三个按钮控件,它们的位置相互关联。第一个按钮位于父容器的顶部和左侧,第二个按钮位于第一个按钮的下方和父容器的右侧,第三个按钮位于第一个按钮和第二个按钮之间,并稍微向右偏移了一些距离。 这样,通过相对布局,我们可以根据控件之间的相对位置来灵活地排列和定位界面元素。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值