Android RelativeLayout布局

article/2025/9/26 5:47:13

1. RelativeLayout类

相对布局(RelativeLayout)将子视图以相对位置显示。默认显示在父视图的左上角。

  • layout_alignParentTop,父视图的上边
  • layout_alignParentBottom,父视图的下边
  • layout_alignParentLeft,父视图的左边
  • layout_alignParentRight,父视图的右边

设置4个子视图在边角位置。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><!-- 左上角显示 --><TextViewandroid:layout_width="100dp"android:layout_height="40dp"android:text="Default"android:gravity="center"android:background="#ffa6a5aa"/><!-- 右上角显示 --><TextViewandroid:layout_width="100dp"android:layout_height="40dp"android:layout_alignParentRight="true"android:text="Right"android:gravity="center"android:background="#ffa6a5aa"/><!-- 左下角显示 --><TextViewandroid:layout_width="100dp"android:layout_height="40dp"android:layout_alignParentBottom="true"android:text="Bottom"android:gravity="center"android:background="#ffa6a5aa"/><!-- 右下角显示 --><TextViewandroid:layout_width="100dp"android:layout_height="40dp"android:layout_alignParentRight="true"android:layout_alignParentBottom="true"android:text="Right|Bottom"android:gravity="center"android:background="#ffa6a5aa"/>
</RelativeLayout>

效果如下
这里写图片描述

2. 居中显示

layout_centerXX可以在父视图内居中显示

  • layout_centerInParent,相对于父视图完全居中
  • layout_centerHorizontal,相对于父视图水平居中
  • layout_centerVertical,相对于父视图垂直居中

可以配合alignParentXX使用

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:layout_width="120dp"android:layout_height="40dp"android:text="centerInParent"android:gravity="center"android:background="#ffa6a5aa"android:layout_centerInParent="true"/><TextViewandroid:layout_width="120dp"android:layout_height="40dp"android:text="centerHorizontal"android:gravity="center"android:background="#ffa6a5aa"android:layout_centerHorizontal="true"/><TextViewandroid:layout_width="120dp"android:layout_height="40dp"android:text="alignParentBottom"android:gravity="center"android:background="#ffa6a5aa"android:layout_centerHorizontal="true"android:layout_alignParentBottom="true"/><TextViewandroid:layout_width="120dp"android:layout_height="40dp"android:text="centerVertical"android:gravity="center"android:background="#ffa6a5aa"android:layout_centerVertical="true"/><TextViewandroid:layout_width="120dp"android:layout_height="40dp"android:text="alignParentRight"android:gravity="center"android:background="#ffa6a5aa"android:layout_centerVertical="true"android:layout_alignParentRight="true"/></RelativeLayout>

效果如下
这里写图片描述

3. 相对视图对齐

  • layout_above,视图的下边与相对视图的上边对齐
  • layout_below,视图的的上边与相对视图的下边对齐
  • layout_toRightOf,视图的左边与相对视图的右边对齐
  • layout_toLeftOf,视图的右边与相对视图的左边对齐

设置的子视图的相对位置,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:id="@+id/tv_center"android:layout_width="150dp"android:layout_height="60dp"android:layout_centerInParent="true"android:text="center"android:background="#ffffcc00"android:gravity="center" /><TextViewandroid:layout_width="100dp"android:layout_height="40dp"android:text="above"android:gravity="center"android:background="#ffa6a5aa"android:layout_above="@id/tv_center"/><TextViewandroid:layout_width="100dp"android:layout_height="40dp"android:text="below"android:gravity="center"android:background="#ffa6a5aa"android:layout_below="@id/tv_center"/><TextViewandroid:layout_width="100dp"android:layout_height="40dp"android:text="toRightOf"android:gravity="center"android:background="#ffa6a5aa"android:layout_toRightOf="@id/tv_center"/><TextViewandroid:layout_width="100dp"android:layout_height="40dp"android:text="toLeftOf"android:gravity="center"android:background="#ffa6a5aa"android:layout_toLeftOf="@id/tv_center"/></RelativeLayout>

效果如下
在这里插入图片描述

4. 边对齐

  • layout_alignTop,视图与基准视图的上边对齐
  • layout_alignBottom:视图与基准视图的下边对齐
  • layout_alignLeft:视图与基准视图的左边对齐
  • layout_alignRight:视图与基准视图的右边对齐
  • layout_alignBaseline:视图与基准视图的基准线对齐

设置的子视图某条边的对齐方式,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:id="@+id/tv_center"android:layout_width="150dp"android:layout_height="100dp"android:layout_centerInParent="true"android:text="center"android:textSize="32sp"android:background="#ffffcc00"android:gravity="center" /><TextViewandroid:layout_width="60dp"android:layout_height="20dp"android:text="alignTop"android:gravity="center"android:background="#ffa6a5aa"android:layout_alignTop="@id/tv_center"/><TextViewandroid:layout_width="60dp"android:layout_height="20dp"android:text="alignBottom"android:gravity="center"android:background="#ffa6a5aa"android:layout_alignBottom="@id/tv_center"/><TextViewandroid:layout_width="60dp"android:layout_height="20dp"android:text="alignLeft"android:gravity="center"android:background="#ffa6a5aa"android:layout_alignLeft="@id/tv_center"/><TextViewandroid:layout_width="60dp"android:layout_height="20dp"android:text="alignRight"android:gravity="center"android:background="#ffa6a5aa"android:layout_alignRight="@id/tv_center"/><TextViewandroid:layout_width="60dp"android:layout_height="20dp"android:text="alignBaseline"android:gravity="center"android:background="#ffa6a5aa"android:layout_alignBaseline="@id/tv_center"/></RelativeLayout>

效果如下
在这里插入图片描述

相关文章
Android LinearLayout布局
Android RelativeLayout布局
Android ConstraintLayout布局
Android 自定义布局


http://chatgpt.dhexx.cn/article/90Qu7vyB.shtml

相关文章

android 继承relativelayout,Android开发中RelativeLayout相对布局

Android开发中RelativeLayout相对布局 RelativeLayout布局是Android界面布局中应用最广也最强大的一种布局&#xff0c;其不只十分灵活&#xff0c;能够解决开发中各类界面布局需求&#xff0c;同时也很方便了解决了多屏幕尺寸的适配问题。在iOS开发中&#xff0c;Autolayout技…

RelativeLayout实现居中偏下x距离引发的小问题

UE想实现一个简单的效果&#xff0c;某个控件在屏幕水平线下方50dp。由于受限于项目历史布局&#xff08;RelativeLayout&#xff09;和一套动态化设置控件位置的方案&#xff0c;竟然遇到了一点问题。&#xff08;在这里还是喊一声&#xff1a;ConstraintLayout最香&#xff0…

RelativeLayout布局

RelativeLayout布局是相对布局&#xff0c;如果RelativeLayout中再包含两个RelativeLayout&#xff0c;不会像LinearLayout一样&#xff0c;宽高一样的话会重叠在一起 将红色布局放到右上角 常见属性 根据父容器定位 layout_alignParentLeft 左对齐 layout_alignParentRig…

RelativeLayout圆角处理

RelativeLayout圆角处理以后&#xff0c;可以变相对子view进行圆角处理&#xff0c;如ImageView&#xff0c;VideoView等 RoundRelativeLayout具体实现 比较简单&#xff0c;只需要在初始化时设置一下layout的ViewOutlineProvider&#xff0c;方便起见&#xff0c;这里写死rad…

RelativeLayout(相对布局)的基本使用

RelativeLayout又称作相对布局&#xff0c;也是一种非常常用的布局。和LinearLayout的排列规则不同&#xff0c;RelativeLayout显得更加随意一些&#xff0c;它可以通过相对定位的方式让控件出现在布局的任何位置。也正因为如此&#xff0c;RelativeLayout中的属性非常多&#…

RelativeLayout相对布局详解

RelativeLayout相对布局在Android UI开发中也应用比较多&#xff0c;虽然它不及LinearLayout使用方便&#xff0c;但某些场景中使用RelativeLayout也是一种很不错的选择。在官网上介绍RelativeLayout是一个视图组&#xff0c;在相对位置显示子视图。RelativeLayout是用于设计用…

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;改变背景背很简单只需要准备一张图片然后指…