RelativeLayout相对布局

news2024/10/7 15:24:13

一、官方地址:

https://developer.android.google.cn/reference/kotlin/android/widget/RelativeLayout?hl=en

二、概述

相对布局(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的正左方

六、示例

一、根据父元素定位九个button
<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=".activity.RelativeLayoutActivity"
    >
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:text="左上角">
    </Button>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:layout_alignParentRight="true"
        android:text="右上角">
    </Button>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:layout_centerHorizontal="true"
        android:text="水平居中">
    </Button>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:layout_centerVertical="true"
        android:text="左居中">
    </Button>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:layout_centerInParent="true"
        android:text="居中">
    </Button>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:text="右居中">
    </Button>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:layout_alignParentBottom="true"
        android:text="左下角">
    </Button>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="下居中">
    </Button>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="右下角">
    </Button>


</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=".activity.RelativeLayoutBrotherActivity"
    >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginTop="@dimen/dp_10"
        android:layout_marginLeft="10dp"
        android:background="#ff0000"
        tools:srcCompat="@drawable/magic" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/imageView"
        android:layout_toRightOf="@+id/imageView"
        android:layout_marginLeft="10dp"
        android:text="关于食品检测仪的使用" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/imageView"
        android:layout_alignBottom="@id/imageView"
        android:layout_marginLeft="10dp"
        android:text="简介:xxxxxxxxx" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/imageView"
        android:layout_alignParentRight="true"
        android:layout_marginRight="10dp"
        android:text="时长:11:40" />
</RelativeLayout>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/463585.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Nacos注册中心的使用

文章目录 Nacos注册中心1. 服务注册到nacos1&#xff09;引入依赖2&#xff09;配置nacos地址3&#xff09;重启 2.服务分级存储模型2.1.给user-service配置集群2.2.同集群优先的负载均衡 3.权重配置 Nacos注册中心 国内公司一般都推崇阿里巴巴的技术&#xff0c;比如注册中心…

Docker安装Redis(普通安装+在线安装+离线安装)

文章目录 Redis概述一、磁盘安装1.1 安装环境1.2 安装步骤1.3 服务器启停命令 二、docker安装1.在线安装2.离线安装 总结 Redis概述 Redis&#xff0c;英文全称是Remote Dictionary Server&#xff08;远程字典服务&#xff09;&#xff0c;是一个开源的使用ANSI C语言编写、支…

ext-1:PDK工具包编译出例程

1、TI的单独StarterWare不更新后&#xff0c;后续维护和更新的是 PROCESSOR-SDK-AM335X 软件开发套件 &#xff08;PDK&#xff09;&#xff0c;对比以前的&#xff0c;里面没有例程&#xff0c;所以下载安装完需要自己编译出example例程。 因为编译出example例程中间会出现很…

设计模式--适配器模式

目录 基本介绍 工作原理 类适配模式 介绍 应用实例介绍 类适配器模式注意事项和细节 对象适配模式 介绍 对象适配器模式注意事项和细节 接口适配器模式 介绍 适配器模式的注意事项和细节 基本介绍 (1) 适配器模式(Adapter Pattern) 将某个类的接口转换成客户端期望的…

从JDK源码级别彻底剖析JVM类加载机制

loadClass的类加载过程 加载 >> 验证 >> 准备 >> 解析 >> 初始化 >> 使用 >> 卸载 ● 加载&#xff1a;在硬盘上查找并通过IO读入字节码文件&#xff0c;使用到类时才会加载&#xff0c;例如调用类的main()方法&#xff0c;new对象等等&am…

如何选择最适合你的数据库解决方案:PostgreSQL VS MySQL 技术选型对比

文章目录 PostgreSQL与MySQL技术选型对比什么是 WordPress 数据库&#xff1f;什么是 PostgreSQL&#xff1f;历史主要特点高度可靠灵活性可扩展性复制用例什么是 MySQL&#xff1f;历史主要特点使用方便高灵活性可靠性和安全性高性能可扩展开源许可证用例PostgreSQL 与 MySQL&…

状态模式——随遇而安

● 状态模式介绍 状态模式中的行为是由状态来决定的&#xff0c;不用的状态下有不同的行为。状态模式和策略模式结构几乎完全一样&#xff0c;但它们的目的、本质却完全不一样就。状态模式的行为是平行的、不可替代的&#xff0c;策略模式的行为是彼此孤立、可相互替换的。用一…

【UE】一个简易的游戏计时器

效果 步骤 1. 打开“ThirdPersonGameMode” 创建两个整型变量&#xff0c;分别命名为“Seconds”、“Minutes” 在事件图表中添加如下节点&#xff0c;实现“Seconds”每秒加1 继续添加如下节点&#xff1a; 当秒数大于60时&#xff0c;就让分钟数1&#xff0c;然后将秒数重新…

P1045 [NOIP2003 普及组] 麦森数

题目描述 形如 2&#xfffd;−12P−1 的素数称为麦森数&#xff0c;这时 &#xfffd;P 一定也是个素数。但反过来不一定&#xff0c;即如果 &#xfffd;P 是个素数&#xff0c;2&#xfffd;−12P−1 不一定也是素数。到 1998 年底&#xff0c;人们已找到了 37 个麦森数。最…

AI数字人技术在高中历史课堂上的应用

引言 介绍AI数字人技术的概念和特点介绍AI数字人技术在教育领域的价值和意义提出本文的主题和目的&#xff1a;探讨AI数字人技术在高中历史课堂上的应用 AI数字人技术在高中历史课堂上的应用方式 介绍AI数字人技术可以通过还原历史人物说话视频&#xff0c;利用历史人物籍贯…

HJHD-91晃电保护器 新款35mm卡轨安装 josef约瑟

名称&#xff1a;晃电保护器品牌&#xff1a;JOSEF约瑟型号&#xff1a;HJHD-91额定电压&#xff1a;110、220VAC触点容量&#xff1a;250V/5A动作时间&#xff1a;不大于20ms功率消耗&#xff1a;不大于5W/5VA HJHD系列晃电保护器 HJHD-91晃电保护器 抗晃电继电器 1.特点和用途…

压力测试工具Jmeter入门

文章目录 一、JMeter概述1、JMeter简介2、JMeter的作用 二、JMeter下载三、JMeter测试1.创建线程组2、配置元件3、为线程添加监听器4、查看报告 一、JMeter概述 1、JMeter简介 Apache JMeter 是 Apache 组织基于 Java 开发的压力测试工具&#xff0c;用于对软件做压力测试。 …

Nginx安装删除

1.卸载Nginx ps -ef|grep nginx 查询Nginx 进程pid kill -9 7035 kill -9 7036 查找根下所有名字包含nginx的文件 find / -name nginx 执行命令 rm -rf *删除nignx安装的相关文件 下面开始安装,安装方式很多,可以选择官网下载后拖进linux 官网nginx: download 官网下载…

Mysql-JSON

一、根据JSON字段检索内容 语法: 使用 字段->$.json属性进行查询条件使用json_extract函数查询&#xff0c;json_extract(字段,"$.json属性")根据json数组查询&#xff0c;用JSON_CONTAINS(字段,JSON_OBJECT(json属性, "内容")) 二、检索查询 1.json…

Python之画一朵玫瑰花

效果&#xff1a; 步骤&#xff1a; 导入turtle库和time库设置画布大小和起始位置绘制红色花瓣&#xff0c;使用begin_fill()函数开始填充&#xff0c;fillcolor()函数设置填充颜色&#xff0c;circle()函数绘制圆形&#xff0c;fd()函数绘制直线&#xff0c;left()和right()函…

【Linux】环境变量和进程优先级

目录 环境变量 什么是环境变量 系统结构 系统接口 深度解析 命令行参数 进程优先级 优先级查看 优先级修改 进程间的概念 环境变量 什么是环境变量 &#x1f36e;平时在使用 Linux 的时候&#xff0c;总会使用 ls 、pwd 这类的命令&#xff0c;我们也都知道这些命令…

C语言从入门到精通第14天(C语言预处理)

C语言预处理 预处理概述宏定义条件编译 预处理概述 在前面我们已经对C语言的基础语法知识有所了解了&#xff0c;每次进行程序的编写之前&#xff0c;我们会使用#include命令去导入我们的库函数&#xff0c;而这种以#号开头的命令称为预处理命令。 C语言提供了多种预处理功能…

MATLAB离散时间信号的实现和时域基本运算(九)

1、实验目的&#xff1a; 1&#xff09;了解时域离散信号的特点&#xff1b; 2&#xff09;掌握MATLAB在时域内产生常用离散时间信号的方法&#xff1b; 3&#xff09;熟悉离散时间信号的时域基本运算&#xff1b; 4&#xff09;掌握离散时间信号的绘图命令。 2、实验内容&…

开放原子训练营(第三季)RT-Thread Nano学习营一探究竟

前言 不知道从什么时候起&#xff0c;智能设备开始普及到了我们日常生活的方方面面。下班还未到家&#xff0c;热水器就可以调到合适的温度&#xff1b;上班刚进公司&#xff0c;忘关空调也不再是什么烦恼&#xff1b;诸如夜晚的灯光变换&#xff0c;白昼的窗帘适应等更给我们…

eBPF的发展演进---从石器时代到成为神(二)

3. 发展溯源 回顾技术的发展过程&#xff0c;就像观看非洲大草原日出日落一样&#xff0c;宏大的过程让人感动&#xff0c;细节部分引人深思。每天循环不辍&#xff0c;却又每天不同。 BPF的应用早已超越了它最初的设计&#xff0c;但如果要追溯BPF最初的来源&#xff0c;则必…