RelativeLayout又称为相对布局,也是一种常用的布局形式。和LinearLayout的排列规则不同,RelativeLayout显得更加随意一下,它通常通过相对定位 的方式让控件出现在布局的任何位置。也正是因为如此,RelativeLayout中的属性非常多,不过这些属性都是有规律可循的。

我们通过代码来看:

relativelayout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button1"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Button 1"/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button2"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button 2"/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button3"
android:layout_centerInParent="true"
android:text="Button 3"/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button4"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Button 4"/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button5"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Button 5"/>
</RelativeLayout>

  运行结果为:

    

由此可见,这些属性和他们的名字一样,分别代表的含义为:

android:layout_alignParentLeft——和父布局的左对齐

android:layout_alignParentTop——和父布局上部对齐

android:layout_alignParentRight——和父布局的右对齐

android:layout_alignParentBottom——和父布局的下部对齐

android:layout_centerInParent——在父布局中居中显示

上面代码我们的控件是以父布局为标准的,在RelativeLayout中,同样也可以以控件为标准进行定位。

代码示例:

relativelayout.xml

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

  运行结果为:

  

这些属性也有规律可循:

android:layout_above——表示该控件在标准控件上方

android:layout_below——表示该控件在标准控件下方

android:layout_toLeftOf——表示该控件在标准控件左面

android:layout_ toRightOf——表示该控件在标准控件右面

但需要注意的是,这些属性需要使用“@id/xxxx”来引入标准控件的id,并且当一个控件以另外一个控件为标准时,另一个控件一定要定义在这个控件前面,否则会出现找不到id的情况。

RelativeLayout中还有另外一组相对于控件进行定位的属性

android:layout_alignRight——表示该控件的右边缘和标准控件的右边缘对齐

android:layout_ alignLeft——表示该控件的左边缘和标准控件的左边缘对齐

android:layout_alignTop——表示该控件的上边缘和标准控件的上边缘对齐 android:layout_alignBottom——表示该控件的下边缘和标准控件的下边缘对齐

Android笔记(八) Android中的布局——相对布局的更多相关文章

  1. Android 笔记之 Android 系统架构

    Android笔记之Android系统架构 h2{ color: #4abcde; } a{ color: blue; text-decoration: none; } a:hover{ color: ...

  2. Android笔记: Android版本号

    由于有2套版本号 总是对应不准 记下来做过标记 Android 4.3 ----18 Android 4.2---17 Android 4.1---16 Android 4.0.3---15Andro ...

  3. Android笔记:java 中的数组

    在与嵌入式设备通讯的过程中使用的socket通讯 获取的字节流,通常转换为字节数组,需要根据协议将字节数组拆分.对于有规律的重复拆分可以使用,由于java中不能像c中直接进行内存操作例如使用struc ...

  4. Android笔记:java 中的枚举

    部分数据使用枚举比较方便,java中的enmu不如c#中使用方便 记录备忘 以c#中的代码为例 public enum PlayState { /// <summary> /// 关闭 / ...

  5. Android笔记之Fragment中创建ViewModel的正确方式

    之前一直都是这么写的 pageViewModel = ViewModelProviders.of(this).get(PageViewModel.class); //参数this是当前fragment ...

  6. Android笔记:android的适配

    public int Dp2Px(Context context, float dp) { final float scale = context.getResources().getDisplayM ...

  7. Android笔记(十一) Android中的布局——网格布局

    网格布局是Android4.0新增的布局管理器,因此需要在Android4.0之后的版本才可以使用,之前的平台使用该布局的话,需要导入相应的支持库. GridLayout的作用类似于HTML中的tab ...

  8. Android笔记(十) Android中的布局——表格布局

    TableLayout运行我们使用表格的方式来排列控件,它的本质依然是线性布局.表格布局采用行.列的形式来管理控件,TableLayout并不需要明确的声明包含多少行多少列,而是通过添加TableRo ...

  9. Android笔记(九) Android中的布局——框架布局

    框架布局没有任何定位方式,所有的控件都会摆放在布局的左上角. 代码示例: framelayout.xml <?xml version="1.0" encoding=" ...

随机推荐

  1. 使用SoapUI测试wsdl

    webservice服务wsdl地址:http://localhost:8080/services/HelloWorld?wsdl new project: new soap project: 调用测 ...

  2. 什么是 https ?这应该是全网把 https 讲的最好的一篇文章了

    https://blog.csdn.net/m0_37907797/article/details/102759257

  3. ready与load的区别

    JQuery里有ready和load事件 $(document).ready(function() { // ...代码... }) //document ready 简写 $(function() ...

  4. java程序cpu问题排查

    方法一: 转载:http://www.linuxhot.com/java-cpu-used-high.html 1.jps 获取Java进程的PID. 2.jstack pid >> ja ...

  5. nodejs中利用expresss脚手架和bootstrap,数据库mongodb搭建的留言板案例

    ## 1. 先打开编辑器,创建一个项目 ## 2. 再打开cmd命令提示符下载express脚手架 express   项目名   --view=ejs 或express   -e    项目名 ## ...

  6. 将自己的项目作为jar包发布到maven中央仓库

    maven版本是3.5.0,jdk是1.8(注意,不是说项目是1.8就行,必须是环境变量里的也是,不能超过1.8,否则一大堆问题,执行mvn前用javac -version看下版本) 一:先在sona ...

  7. Linux 脚本语言入门

    0.脚本编写初步介绍 (1)脚本第一行以 #!/bin/sh 开始,也可以用 #!/bin/bash 开始,但是第一行必须以这种方式开始. (2)脚本名需要以.sh结尾 (3)#开头的句子表示注释 ( ...

  8. [转帖]Epoll和IOCP的比较

    Epoll和IOCP的比较 https://yq.aliyun.com/articles/53621/   kryptosx 2016-05-27 23:32:51 浏览6181   原来整理过一个& ...

  9. [ClickOnce] - Win10 管理员模式下无法安装 ClickOnce 之解决

    Issue Windows 10 管理员模式下,点击 ClickOnce 安装程序无反应. 解决 1. 按 WIN+R 键打开“运行”窗口,输入 “gpedit.msc" 打开组策略.2.  ...

  10. scau 9502 ARDF一个变量的问题

    哨兵变量flag不小心没 设置成0..所以一直WA 9502 ARDF 时间限制:1000MS  内存限制:65535K 提交次数:0 通过次数:0 题型: 编程题   语言: G++;GCC Des ...