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. 《楞严经四种清净明诲》 (转自学佛网:http://www.xuefo.net/nr/article56/559965.html)

    <楞严经四种清净明诲> 佛告阿难:“汝常闻我毗奈耶中,宣说修行三决定义.所谓摄心为戒,因戒生定,因定发慧,是则名为三无漏学. “阿难,云何摄心,我名为戒? “若诸世界六道众生其心不淫,则不 ...

  2. 05点睛Spring MVC 4.1-服务器端推送

    转发:https://www.iteye.com/blog/wiselyman-2214626 5.1 服务器端推送 SSE(server send event)是一种服务器端向浏览器推送消息的技术, ...

  3. Framework7 介绍

    Framework7 - is a free and open source framework to develop mobile, desktop or web apps with native ...

  4. DSOFramer 控件(转)

    1.Html电子印章.手写签名系统演示:http://www.dianju.com.cn/video.htm在线试用:http://www.dianju.com.cn/websignpiaoju/ht ...

  5. mysql order by rand() 优化方法

    mysql order by rand() 优化方法 适用于领取奖品等项目<pre>mysql> select * from user order by rand() limit 1 ...

  6. SQL优化——select

    MYSQL优化实施方案:https://www.cnblogs.com/clsn/p/8214048.html 对查询时经常用到的字段建立索引,如包含多个也可以构建复合索引,建立索引之后需要注意的一点 ...

  7. TCP/UDP网络编程调试助手下载

    下载地址:可能需要谷歌:软件干净,挺好用的,如果有更好的,欢迎留言! https://www.waveshare.com/wiki/File:TCP-UDP-Debug.7z

  8. 《ucore lab8》实验报告

    资源 ucore在线实验指导书 我的ucore实验代码 练习1: 完成读文件操作的实现(需要编码) 题目 首先了解打开文件的处理流程,然后参考本实验后续的文件读写操作的过程分析,编写在sfs_inod ...

  9. 028 Android 旋转动画+病毒查杀效果+自定义样式的ProgressBar

    1.目标效果 旋转动画+病毒查杀效果 2.xml布局文件 (1)activity_kill_virus.xml <?xml version="1.0" encoding=&q ...

  10. 传输json数据到前台的时候,数据中包含日期数据

    问题描述 当从数据库中查询的数据中包含有日期格式的数据的时候,数据传输到前台会报错. 解决方式 // 逐条将日期进行格式化后再传输 Date date = new SimpleDateFormat(& ...