当前位置: 首页 > 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.lryc.cn/news/607421.html

相关文章:

  • AI在安全方面的十个应用场景
  • 技术栈:基于Java语言的搭子_搭子社交_圈子_圈子社交_搭子小程序_搭子APP平台
  • 电子合同管理台账功能详解
  • 移除 Excel 文件(.xlsx)的工作表保护
  • EasyExcel 公式计算大全
  • python进程、线程、协程
  • 【LeetCode 热题 100】155. 最小栈
  • 【东枫科技】DreamHAT+
  • 【人工智能-17】机器学习:KNN算法、模型选择和调优、朴素贝叶斯分类
  • kafka快速部署、集成、调优
  • 力扣 hot100 Day62
  • 机器学习sklearn:编码、哑变量、二值化和分段
  • TCP协议的特点和首部格式
  • 同品牌的系列广告要如何保证宣传的连贯性?
  • 广东省省考备考(第六十三天8.1)——判断推理(强化训练)
  • 国产开源大模型崛起:使用Kimi K2/Qwen2/GLM-4.5搭建编程助手
  • Galaxea机器人由星海图人工智能科技有限公司研发的高性能仿人形机器人
  • 大模型结构比较
  • uniapp 开发微信小程序,获取经纬度(uni.getLocation)并且转化详细地址(‌高德地图逆地理编码API、‌腾讯地图逆地理编码)
  • SIP 呼叫中实现远端摄像头控制学习笔记
  • axios请求的取消
  • 什么是链游
  • Spring Boot Actuator 保姆级教程
  • JavaWeb--Student2025项目:增删改查
  • 七彩喜艾灸机器人:让传统艾灸变简单,健康养生触手可及
  • HarmonyOS 应用拉起系列(一):应用与元服务互通方式
  • 乐观锁是数据库和多线程编程中常用的一种控制并发的方法
  • 【数据可视化-77】中国历年GDP数据可视化分析:Python + Pyecharts 深度洞察(含完整数据、代码)
  • 伞状Meta分析重构癌症幸存者照护指南:从矛盾证据到精准决策
  • OSPF综合实验报告册