Android Studio:相对布局 RelativeLayout

devtools/2025/2/8 11:26:01/

        在 Android 中,RelativeLayout 是一种布局,它允许你根据其他视图的位置来相对地定位视图。

        以下面的代码为例:

案例一:

 android:layout_alignParentTop="true": 将视图的顶部对齐到父容器的顶部。

android:layout_centerHorizontal="true": 将视图水平居中。

文本视图中的android:layout_below="@id/button1": 将视图放置在另一个视图(在此例中是 Button)的下方。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><!-- 按钮,顶部对齐父容器,并水平居中 --><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button 1"android:layout_alignParentTop="true"android:layout_centerHorizontal="true" /><!-- 文本视图,位于按钮下方,水平居中 --><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello, World!"android:layout_below="@id/button1"android:layout_centerHorizontal="true"android:layout_marginTop="20dp" />

android:layout_alignParentBottom="true": 将视图的底部对齐父容器的底部。

android:layout_alignParentRight="true": 将视图的右侧对齐父容器的右侧。

android:layout_marginBottom="50dp"android:layout_marginRight="50dp": 为视图的底部和右侧添加间距。

<!-- 一个按钮,位于图片视图的右侧,并且对齐父容器的底部 --><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button 2"android:layout_alignParentBottom="true"android:layout_alignParentRight="true"android:layout_marginBottom="50dp"android:layout_marginRight="50dp" />
  • android:layout_toLeftOf="@id/imageView1": 将视图放置在指定视图(imageView1)的左侧。
  • android:layout_alignTop="@id/imageView1": 将视图与指定视图(imageView1)的顶部对齐。
<!-- 一个文本视图,位于图片视图的左侧 --><TextViewandroid:id="@+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Another Text"android:layout_toLeftOf="@id/imageView1"android:layout_alignTop="@id/imageView1"android:layout_marginRight="10dp" /><!-- 一个按钮,位于文本视图的下方 --><Buttonandroid:id="@+id/button3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button 3"android:layout_below="@id/textView2"android:layout_centerHorizontal="true"android:layout_marginTop="15dp" />

总体效果图如下:

再看一个案例:

案例二:

先给出效果图

完整代码如下:

线性布局作为根,垂直方向排列,android:padding="5dp"设置了内边距,也就是视图内容上下左右各压缩了5dp的显示区域。

线性布局内是5个相对布局组件,gravity="left|center" 具体含义是:内容会水平靠左,同时垂直居中。通过5个相对布局组件,可以实现内容的自由排列。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:padding="5dp" ><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="40dp" ><TextViewandroid:id="@+id/tv_name"android:layout_width="wrap_content"android:layout_height="match_parent"android:gravity="center"android:text="姓名:"android:textColor="@color/black"android:textSize="17sp" /><EditTextandroid:id="@+id/et_name"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginBottom="3dp"android:layout_marginTop="3dp"android:layout_toRightOf="@+id/tv_name"android:background="@drawable/editext_selector"android:gravity="left|center"android:hint="请输入姓名"android:inputType="text"android:maxLength="12"android:textColor="@color/black"android:textSize="17sp" /></RelativeLayout><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="40dp" ><TextViewandroid:id="@+id/tv_age"android:layout_width="wrap_content"android:layout_height="match_parent"android:gravity="center"android:text="年龄:"android:textColor="@color/black"android:textSize="17sp" /><EditTextandroid:id="@+id/et_age"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginBottom="3dp"android:layout_marginTop="3dp"android:layout_toRightOf="@+id/tv_age"android:background="@drawable/editext_selector"android:gravity="left|center"android:hint="请输入年龄"android:inputType="number"android:maxLength="2"android:textColor="@color/black"android:textSize="17sp" /></RelativeLayout><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="40dp" ><TextViewandroid:id="@+id/tv_height"android:layout_width="wrap_content"android:layout_height="match_parent"android:gravity="center"android:text="身高:"android:textColor="@color/black"android:textSize="17sp" /><EditTextandroid:id="@+id/et_height"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginBottom="3dp"android:layout_marginTop="3dp"android:layout_toRightOf="@+id/tv_height"android:background="@drawable/editext_selector"android:gravity="left|center"android:hint="请输入身高"android:inputType="number"android:maxLength="3"android:textColor="@color/black"android:textSize="17sp" /></RelativeLayout><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="40dp" ><TextViewandroid:id="@+id/tv_weight"android:layout_width="wrap_content"android:layout_height="match_parent"android:gravity="center"android:text="体重:"android:textColor="@color/black"android:textSize="17sp" /><EditTextandroid:id="@+id/et_weight"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginBottom="3dp"android:layout_marginTop="3dp"android:layout_toRightOf="@+id/tv_weight"android:background="@drawable/editext_selector"android:gravity="left|center"android:hint="请输入体重"android:inputType="numberDecimal"android:maxLength="5"android:textColor="@color/black"android:textSize="17sp" /></RelativeLayout><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="40dp" ><CheckBoxandroid:id="@+id/ck_married"android:layout_width="wrap_content"android:layout_height="match_parent"android:gravity="center"android:checked="false"android:text="已婚"android:textColor="@color/black"android:textSize="17sp" /></RelativeLayout><Buttonandroid:id="@+id/btn_save"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="保存到全局内存"android:textColor="@color/black"android:textSize="17sp" /></LinearLayout>


http://www.ppmy.cn/devtools/157080.html

相关文章

verilog练习:i2c slave 模块设计

文章目录 前言1. 结构2.代码2.1 iic_slave.v2.2 sync.v2.3 wr_fsm.v2.3.1 状态机状态解释 2.4 ram.v 3. 波形展示4. 建议5. 资料总结 前言 首先就不啰嗦iic协议了&#xff0c;网上有不少资料都是叙述此协议的。 下面将是我本次设计的一些局部设计汇总&#xff0c;如果对读者有…

数据挖掘常用算法

文章目录 基于机器学习~~线性/逻辑回归~~树模型~~贝叶斯~~~~聚类~~集成算法神经网络~~支持向量机~~~~降维算法~~ 基于机器学习 线性/逻辑回归 类似单层神经网络 yk*xb 树模型 优点 可以做可视化分析速度快结果稳定 依赖前期对业务和数据的理解 贝叶斯 贝叶斯依赖先验概…

python编程-内置函数bin(),bool(),abs() ,all(),any(),ascii(),max(),min() 详解

1、bin()函数用于将整数转换为其二进制字符串表示。并返回一个以0b开头的字符串&#xff0c;表示该整数的二进制形式。 # 十进制数转换为二进制字符串 decimal_number 42 binary_string bin(decimal_number) print(f"Decimal {decimal_number} is {binary_string} in b…

YOLOv11实时目标检测 | 摄像头视频图片文件检测

在上篇文章中YOLO11环境部署 || 从检测到训练https://blog.csdn.net/2301_79442295/article/details/145414103#comments_36164492&#xff0c;我们详细探讨了YOLO11的部署以及推理训练&#xff0c;但是评论区的观众老爷就说了&#xff1a;“博主博主&#xff0c;你这个只能推理…

数据结构与算法(test1)

一、树和二叉树 1. 看图&#xff0c;完成以下填空 (1).树的度为________。 (2).树中结点的最大层次&#xff0c;称为树的_____或树的______&#xff0c;值是______。 (3).结点A和B的度分别为________ 和 ________。 (4).结点A是结点B的________。 (5).结点B是结点A的________…

在线影视播放网站PHP电影网站源码自动采集MKCMS升级版米酷模板含WAP手机版附三套模板

在线影视播放网站PHP电影网站源码自动采集MKCMS升级版米酷模板含WAP手机版附三套模板 所有视频均自动更新在线播放&#xff0c;不用任何人工操作&#xff0c;懒人必备。内含三套模板&#xff0c;支持手机版。 将源码上传到空间 (PHP5.3~5.6 版本)&#xff0c;搜索不能用的&am…

PyTorch Geometric(PyG)机器学习实战

PyTorch Geometric&#xff08;PyG&#xff09;机器学习实战 在图神经网络&#xff08;GNN&#xff09;的研究和应用中&#xff0c;PyTorch Geometric&#xff08;PyG&#xff09;作为一个基于PyTorch的库&#xff0c;提供了高效的图数据处理和模型构建功能。 本文将通过一个节…

vim-plug的自动安装与基本使用介绍

vim-plug介绍 Vim-plug 是一个轻量级的 Vim 插件管理器&#xff0c;它允许你轻松地管理 Vim 插件的安装、更新和卸载。相较于其他插件管理器&#xff0c;vim-plug 的优点是简单易用&#xff0c;速度较快&#xff0c;而且支持懒加载插件&#xff08;即按需加载&#xff09; 自动…