RelativeLayout相对布局详解

article/2025/9/26 5:52:23

RelativeLayout相对布局在Android UI开发中也应用比较多,虽然它不及LinearLayout使用方便,但某些场景中使用RelativeLayout也是一种很不错的选择。在官网上介绍RelativeLayout是一个视图组,在相对位置显示子视图。RelativeLayout是用于设计用户界面的非常强大的实用程序,因为它可以消除嵌套视图组并保持布局层次结构平坦,从而提高性能。如果您发现自己使用了多个嵌套LinearLayout组,则可以将它们替换为单个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>

创建了一个RelativeLayout(相对布局),宽、高为match_parent(匹配父级宽高)。

根据父容器定位

RelativeLayout相对布局子项根据父容器定位,可以轻松确定子项位置。

  • 左对齐: android:layout_alighParentStart

  • 右对齐: android:layout_alighParentEnd

  • 顶端对齐: android:layout_alighParentTop

  • 底部对齐: android:layout_alighParentBottom

  • 水平居中: android:layout_centerHorizontal

  • 垂直居中: android:layout_centerVertical

  • 中央位置: android: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"><TextViewandroid:text="alignParentTop"android:textColor="@color/black"android:textSize="18sp"android:layout_alignParentTop="true"android:layout_centerHorizontal="true"android:layout_width="wrap_content"android:layout_height="wrap_content"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_centerHorizontal="true"android:text="alignParentBottom"android:textColor="@color/black"android:textSize="18sp" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentStart="true"android:text="alignParentStart"android:layout_centerVertical="true"android:textColor="@color/black"android:textSize="18sp" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentEnd="true"android:text="alignParentEnd"android:layout_centerVertical="true"android:textColor="@color/black"android:textSize="18sp" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="centerInParent"android:textColor="@color/black"android:textSize="18sp" />
</RelativeLayout>

上面布局嵌套了五个TextView子项,其中一个位于父容器中央,其余四个通过设置分别位于父容器上下左右四边居中。

根据兄弟容器定位

子项除了可以根据父容器定位,还可以根据兄弟子项实现定位,需要指定参考子项的id。

  • 左边: android:layout_toStartOf

  • 右边: android:layout_toEndOf

  • 上方: android:layout_above

  • 下方: android:layout_below

  • 对齐上边界: android:layout_alignTop

  • 对齐下边界: android:layout_alignBottom

  • 对齐左边界: android:layout_alignStart

  • 对齐右边界: android:layout_alignEnd

下面代码实现一个梅花布局效果:

<?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"><ImageViewandroid:id="@+id/center"android:layout_width="50dp"android:layout_height="50dp"android:src="@mipmap/ic_launcher"android:layout_centerInParent="true"/><!--最中间--><ImageViewandroid:layout_width="50dp"android:layout_height="50dp"android:src="@mipmap/ic_launcher"android:layout_above="@+id/center"android:layout_centerHorizontal="true"/><!--上面中间--><ImageViewandroid:layout_width="50dp"android:layout_height="50dp"android:src="@mipmap/ic_launcher"android:layout_below="@+id/center"android:layout_centerHorizontal="true"/><!--下面中间--><ImageViewandroid:layout_width="50dp"android:layout_height="50dp"android:src="@mipmap/ic_launcher"android:layout_toStartOf="@+id/center"android:layout_centerVertical="true"/><!--左边中间--><ImageViewandroid:layout_width="50dp"android:layout_height="50dp"android:src="@mipmap/ic_launcher"android:layout_toEndOf="@+id/center"android:layout_centerVertical="true"/><!--右边中间-->
</RelativeLayout>

效果预览:

上面示例首先确定中间位置,再以中间为参考,确定四边的位置。如果需要每个子项之间有一定的间距,只需要给最中间的那位设置layout_margin即可。

<ImageViewandroid:id="@+id/center"android:layout_width="50dp"android:layout_height="50dp"android:layout_margin="10dp"android:src="@mipmap/ic_launcher"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"><RelativeLayoutandroid:id="@+id/c3"android:layout_width="match_parent"android:layout_height="wrap_content"android:paddingHorizontal="36dp"android:paddingVertical="18dp"android:layout_above="@+id/c2"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="7"android:textSize="36sp"android:textColor="@color/black"android:textStyle="bold"android:layout_alignParentStart="true"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="8"android:textSize="36sp"android:textColor="@color/black"android:textStyle="bold"android:layout_centerHorizontal="true"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="9"android:textSize="36sp"android:textColor="@color/black"android:textStyle="bold"android:layout_alignParentEnd="true"/></RelativeLayout><RelativeLayoutandroid:id="@+id/c2"android:layout_width="match_parent"android:layout_height="wrap_content"android:paddingHorizontal="36dp"android:paddingVertical="18dp"android:layout_above="@+id/c1"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="4"android:textSize="36sp"android:textColor="@color/black"android:textStyle="bold"android:layout_alignParentStart="true"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="5"android:textSize="36sp"android:textColor="@color/black"android:textStyle="bold"android:layout_centerHorizontal="true"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="6"android:textSize="36sp"android:textColor="@color/black"android:textStyle="bold"android:layout_alignParentEnd="true"/></RelativeLayout><RelativeLayoutandroid:id="@+id/c1"android:layout_width="match_parent"android:layout_height="wrap_content"android:paddingHorizontal="36dp"android:paddingVertical="18dp"android:layout_above="@+id/c0"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="1"android:textSize="36sp"android:textColor="@color/black"android:textStyle="bold"android:layout_alignParentStart="true"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="2"android:textSize="36sp"android:textColor="@color/black"android:textStyle="bold"android:layout_centerHorizontal="true"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="3"android:textSize="36sp"android:textColor="@color/black"android:textStyle="bold"android:layout_alignParentEnd="true"/></RelativeLayout><RelativeLayoutandroid:id="@+id/c0"android:layout_width="match_parent"android:layout_height="wrap_content"android:paddingHorizontal="36dp"android:paddingVertical="18dp"android:layout_alignParentBottom="true"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="*"android:textSize="36sp"android:textColor="@color/black"android:textStyle="bold"android:layout_alignParentStart="true"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="0"android:textSize="36sp"android:textColor="@color/black"android:textStyle="bold"android:layout_centerHorizontal="true"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="#"android:textSize="36sp"android:textColor="@color/black"android:textStyle="bold"android:layout_alignParentEnd="true"/></RelativeLayout></RelativeLayout>

好吧,代码有点长,拨号键和删除键也没有,有兴趣的读者可以自行完善一下,当然实现的方法也不止一种。


作者:微语博客
链接:https://www.jianshu.com/p/a26a8906239a
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


http://chatgpt.dhexx.cn/article/OLZw7YYJ.shtml

相关文章

Android相对布局(RelativeLayout)

Android相对布局(RelativeLayout) 备注&#xff1a;这里的视图和元素是等同的概念。 RelativeLayout是一个允许子视图相对于其他兄弟视图或是父视图显示的视图组(通过ID指定)。每个视图的位置能够指定它相对于兄弟(比如在其他视图的左边或是下边)或是父视图(这里是指相对布局容…

【Android】相对布局(RelativeLayout)最全解析

【Android】相对布局&#xff08;RelativeLayout&#xff09;最全解析 一、相对布局&#xff08;RelativeLayout&#xff09;概述二、根据父容器定位三、根据兄弟控件定位 一、相对布局&#xff08;RelativeLayout&#xff09;概述 相对布局&#xff08;RelativeLayout&#x…

Flutter ListView详解

ListView详解 ListView常用构造ListViewListView 默认构建效果 ListView ListTileListTile 属性ListTile 使用效果 ListView builderbuilder属性详细介绍分析几个比较难理解的属性效果builder模式来设置分割线 ListView separatedseparatorBuilderseparated设置分割线效果 List…

UE4 ListView

UE4-ListView UE4ListView和U3D的思路不太一样&#xff0c;详细了解之后发现UE4的ListView还是蛮先进的&#xff0c;直接就实现了逻辑和显示分离&#xff0c;提高效率&#xff0c;相对的&#xff0c;他的用法也比Unity的ListView绕一些。举例&#xff0c;一个ListView如果设置…

Android 控件 —— ListView

ListView 的简单用法 在布局中加入 ListView 控件还算简单&#xff0c;先为 ListView 指定一个 id&#xff0c;然后将宽度和高度都设置为 match_parent&#xff0c;这样 ListView 就占满了整个布局的空间 <LinearLayout xmlns:android"http://schemas.android.com/ap…

ListView 组件

简介&#xff1a; ListView是最常用的可滚动组件之一 有三种构建方式&#xff1a; ListViewListView.builderListView.separated 主要参数说明&#xff1a; scrollDirection: Axis.horizontal 水平列表Axis.vertical 垂直列表 padding: 内边距 resolve: 组件反向排序 childr…

4.ListView

ListView 文章目录 ListView一、什么是ListView二、ListView入门1.ListView核心类2.代码编写步骤 三、ListView优化四、把复杂界面(通过xml文件实现)显示到ListView上1.View的静态方法2.获取LayoutInflater对象 五、SimpleAdapter & ArrayAdapter的使用1.ArrayAdapter2.Sim…

ListView及ListAdapter详解

ListView及ListAdapter详解 一、AdapterView 1. 简介 An AdapterView is a view whose children are determined by an Adapter. 简单地说就是其子视图是由适配器决定的视图组件 2. 子类 ListViewGridViewSpinnerGallery3. 常用方法 //功能:获取list中指定位置item get…

qt listview

运行图 .h #ifndef WIDGET_H #define WIDGET_H#include <QWidget>#include <QStringListModel> #include <QModelIndex>namespace Ui { class Widget; }class Widget : public QWidget {Q_OBJECTpublic:explicit Widget(QWidget *parent 0);~Widget();pri…

flutter 之 ListView的使用与详解 map for listview.builder 的使用

1.ListView 配合ListTile 实现新闻列表样式 ListView(children: <Widget>[ListTile(title: const Text(我是 title),subtitle: const Text(我是 sub_title),leading: Image.asset(images/c.png,fit: BoxFit.cover,),trailing: const Icon(Icons.chevron_right,size: 22,…

android ListView

android ListView几个比较特别的属性 由于这两天在做listView的东西&#xff0c;所以整理出来一些我个人认为比较特别的属性&#xff0c;通过设置这样的属性可以做出更加美观的列表 首先是stackFromBottom属性&#xff0c;这只该属性之后你做好的列表就会显示你列表的最下面&am…

ListView使用总结

ListView使用总结 虽然随着RecyclerView的不断普及&#xff0c;相应的资源也越来越多&#xff0c;许多的项目都在使用RecyclerView&#xff0c;但作为他的前辈ListView&#xff0c;加深对ListView的使用有助于我们更好的适应到RecyclerView的使用中。 首先看一下我们实现的效…

Android有关ListView嵌套ListView的一些问题

本人在做评论回复功能的时候&#xff0c;查阅到ListView结合Adapter适配器具有以列表的形式 展示具体数据内容&#xff0c;并且能够根据数据的长度自适应屏幕显示的功能&#xff0c;因此打算在ListView中嵌套ListView完成点击事件后弹出输入框再输入数据后在下方显示回复内容&a…

ListView详解0

ListView常用方法总结 1、listview拖动变黑解决方法 在Android中&#xff0c;ListView是最常用的一个控件&#xff0c;在做UI设计的时候&#xff0c;很多人希望能够改变一下它的背景&#xff0c;使他能够符合整体的UI设计&#xff0c;改变背景背很简单只需要准备一张图片然后指…

C# ListView 的用法

ListView 是一种多列的列表视图控件&#xff0c;可以用于展示多个数据项及其相关信息。ListView 控件提供了丰富的属性和事件&#xff0c;可以用于实现各种各样的表格视图&#xff0c;包括带有单元格编辑、排序和分组等功能。 下面我们通过几个示例来演示如何使用 ListView 控…

Qt ListView使用

概述 Qt中ListView加载数据一般有两种方式&#xff0c;一种是直接qml文件中model直接定义并且放置数据&#xff0c;一种是C代码中定义Model&#xff0c;再setContextProperty的方式暴露给qml域。 步骤 &#xff08;1&#xff09;qml界面 import QtQuick 2.0 import QtQui…

Android控件listview ListView的用法

在Android开发中&#xff0c;ListView是一个比较常用的控件&#xff0c;它以列表的形式展示数据内容&#xff0c;并且能够根据列表的高度自适应屏幕显示。ListView的样式是由属性决定的&#xff0c;它的常用属性如下所示 android:listSelector 点击后改变背景颜色 android:divi…

Android之ListView实现

ListView 用来显示多个可滑动项&#xff08;Item&#xff09;列表的ViewGroup。 需要使用Adapter&#xff08;适配器&#xff09;将集合数据和每一个Item所对应的布局动态适配到ListView中显示 显示列表&#xff1a;listView.setAdapter(adapter) Adapter ArrayAdapter&#xf…

ListView使用方法

ListView使用方法总结 - 直接使用ListView组件创建列表 - 通过Activity继承ListActivity创建 - 定制ListView界面 直接使用ListView组件创建列表 通过数组资源文件指定列表项 先在XML布局文件中添加ListView标志&#xff0c;设置好相关属性&#xff1b;在values下创建数组资…

Android—— ListView 的简单用法及定制ListView界面

一、ListView的简单用法 2. 训练目标 1) 掌握 ListView 控件的使用 2) 掌握 Adapter 桥梁的作用 实现步骤&#xff1a; 1&#xff09;首先新建一个项目&#xff0c; 并让ADT 自动帮我们创建好活动。然后修改activity_main.xml 中的代码&#xff0c;如下所示&#xff1a; &…