当前位置: 首页 > news >正文

安卓开发--RelativeLayout(相对布局)

特点:是 Android 中一种基于相对位置进行布局的容器,它允许子视图通过相对于父容器或其他子视图的位置关系来定位。

基本属性

1. 相对于父容器定位

属性

说明

android:layout_alignParentTop

与父容器顶部对齐

android:layout_alignParentBottom

与父容器底部对齐

android:layout_alignParentLeft

与父容器左侧对齐

android:layout_alignParentRight

与父容器右侧对齐

android:layout_centerInParent

在父容器中居中

android:layout_centerHorizontal

水平居中

android:layout_centerVertical

垂直居中

2. 相对于其他视图定位

属性

说明

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_alignBaseline

与指定视图的基线对齐

3. 边距设置

属性

说明

android:layout_margin

所有方向外边距

android:layout_marginTop

上外边距

android:layout_marginBottom

下外边距

android:layout_marginLeft

左外边距

android:layout_marginRight

右外边距

android:layout_marginStart

起始边外边距

android:layout_marginEnd

结束边外边距

4.centerInParentcenterHorizontal, 和 centerVertical

  • android:layout_centerInParent:将视图水平和垂直居中对齐。
  • android:layout_centerHorizontal:将视图水平居中。
  • android:layout_centerVertical:将视图垂直居中。

实例:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"xmlns:app="http://schemas.android.com/apk/res-auto"tools:context=".MainActivity"android:id="@+id/main"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:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:text="Button 1"/><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_alignParentTop="true"android:text="Button 2"/><Buttonandroid:id="@+id/button3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="Button 3"/><Buttonandroid:id="@+id/button4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentBottom="true"android:text="Button 4"/><Buttonandroid:id="@+id/button5"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_alignParentBottom="true"android:text="Button 5"/></RelativeLayout>

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"xmlns:app="http://schemas.android.com/apk/res-auto"tools:context=".MainActivity"android:id="@+id/main"android:layout_width="match_parent"android:layout_height="match_parent"><Buttonandroid:id="@+id/button3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="Button 3"/><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_above="@id/button3"android:layout_toLeftOf="@id/button3"android:text="Button 1"/><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_above="@id/button3"android:layout_toRightOf="@id/button3"android:text="Button 2"/><Buttonandroid:id="@+id/button4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/button3"android:layout_toLeftOf="@id/button3"android:text="Button 4"/><Buttonandroid:id="@+id/button5"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/button3"android:layout_toRightOf="@id/button3"android:text="Button 5"/></RelativeLayout>

http://www.dtcms.com/a/310198.html

相关文章:

  • 数论:卢卡斯定理
  • 计算机网络:组播和多播有什么区别?
  • ESD监控系统确保工厂生产设备的静电安全
  • 机试备考笔记 1/31
  • 【科普】怎么理解Modbus、TCP、UDP
  • JavaWeb笔记2-JavaScriptVueAjax
  • MATLAB的三维重建系统
  • 从 0 到 1 认识 Spring MVC:核心思想与基本用法(上)
  • CIU32L030=CW32L010 PIN=PIN免费送样,开发板
  • 【论文学习】KAG论文翻译
  • 计算机基础速通--数据结构·线性表应用
  • RA4M2_MINI驱动OLED(1)----驱动OLED
  • LangChain框架入门04:10分钟优雅接入主流大模型
  • 智能Agent场景实战指南 Day 28:Agent成本控制与商业模式
  • 常见框架漏洞
  • dify
  • SSL 剥离漏洞
  • vue2实现类似chatgpt和deepseek的AI对话流打字机效果,实现多模型同时对话
  • OpenCV-python小玩意2 摄像头预览时画线以及line的用法
  • Vue 引入字体文件YouSheBiaoTiHei
  • VMware 下 Ubuntu 操作系统下载与安装指南
  • 前沿智能推荐算法:基于多模态图神经网络的隐私保护推荐系统
  • 学习笔记:原子操作与锁以及share_ptr的c++实现
  • 调试 Rust 生成的 WebAssembly
  • OSPF综合实验报告册
  • 1 前言:什么是 CICD 为什么要学 CICD
  • vue打包后如何在本地运行?
  • sqli-labs:Less-17关卡详细解析
  • 数据结构:在链表中查找(Searching in a Linked List)
  • PyTorch分布式训练深度指南