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

article/2025/9/26 6:03:37

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

  • 一、相对布局(RelativeLayout)概述
  • 二、根据父容器定位
  • 三、根据兄弟控件定位

一、相对布局(RelativeLayout)概述

相对布局(RelativeLayout)是一种根据父容器和兄弟控件作为参照来确定控件位置的布局方式。
在这里插入图片描述

使用相对布局,需要将布局节点改成RelativeLayout,基本格式如下:

 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity">....</RelativeLayout>

二、根据父容器定位

在相对布局中,可以通过以下的属性让的组合让控件处于父容器左上角、右上角、左下角、右下角、上下左右居中,正居中等九个位置。属性如下:

  • android:layout_alignParentLeft="true" 父容器左边
  • android:layout_alignParentRight="true" 父容器右边
  • android:layout_alignParentTop="true" 父容器顶部
  • android:layout_alignParentBottom="true" 父容器底部
  • android:layout_centerHorizontal="true" 水平方向居中
  • android:layout_centerVertical="true" 垂直方向居中
  • android:layout_centerInParent="true" 水平垂直都居中
    在这里插入图片描述
    举例:给一个控件添加 android:layout_alignParentRight="true" 和- android:layout_alignParentTop="true" 属性后该控件处于父容器右上角在这里插入图片描述

给一个控件添加 android:layout_alignParentLeft="true"android:layout_centerVertical="true" 属性后该控件处于父容器左边垂直居中位置
在这里插入图片描述

三、根据兄弟控件定位

在相对布局中,还支持通过已确定位置的控件作为参考来确定其他控件的位置,以下的属性让的组合让控件处于另外控件左上角、右上角、左下角、右下角、正上方、正下方、正左方、正右方等位置。属性如下:

  • android:layout_toLeftOf="@+id/button1" 在button1控件左方

  • android:layout_toRightOf="@+id/button1" 在button1控件右方

  • android:layout_above="@+id/button1" 在button1控件上方

  • android:layout_below="@+id/button1" 在button1控件下方

  • android:layout_alignLeft="@+id/button1" 与button1控件左边平齐

  • android:layout_alignRight="@+id/button1" 与button1控件右边平齐

  • android:layout_alignTop="@+id/button1" 与button1控件上边平齐

  • android:layout_alignBottom="@+id/button1" 与button1控件下边平齐
    在这里插入图片描述

给一个控件添加 android:layout_toLeftOf="@+id/button1"android:layout_below="@+id/button1" 属性后该控件处于button1的左下方位置
在这里插入图片描述

给一个控件添加 android:layout_toLeftOf="@+id/button1"layout_alignTop="@+id/button1" 属性后该控件处于button1的正左方
在这里插入图片描述


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

相关文章

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; &…

QT listView学习

文章目录 listViewdemo说明demo演示model定义委托 QStyledItemDelegate总结 listView listView 对比 tableView 、 treeView来说&#xff0c;最大的不同就是数据结构的不同。treeView是像树一样的层次结构&#xff0c;而listView则就是像链表一样的结构 跟之前的treeView&…

还在用ListView?

还在用Lisview&#xff1f;RecyclerView都已经出来一年多了&#xff01; 想必大家多或多或少的接触过或者了解过RecyclerView&#xff0c;为什么没有用起来&#xff0c;原因大概如下&#xff1f; ListView我用的挺好的&#xff0c;为什么要换RecyclerView&#xff1f;ListView…