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. linux生成新的列(awk)

    awk的功能特别强大,其中一个功能就是生成新的列,不论这列是在第一列,最后一列,还是中间,随你任性插入. 举例来说,有下列文件test1.txt test1.txt a b c 1 2 3 现在我想在 ...

  2. Django中的QuerySet查询优化之prefetch_related

    转载的,做个笔记,原文链接 在数据库有外键的时候,使用 select_related() 和 prefetch_related() 可以很好的减少数据库请求的次数,从而提高性能.本文通过一个简单的例子 ...

  3. Swift4.0复习扩展

    1.扩展计算式属性: 2.对方法进行扩展: /// 定义枚举类型Light, /// 它指定了基本类型String enum Light: String { case red = "red& ...

  4. LODOP打印超文本字符串拼接2 单选选择css样式表格

    之前的相关字符串拼接的博文:LODOP打印超文本字符串拼接1 固定表格填充数值之前博文介绍过,字符串可以随意拼接,只要最后组织成的字符串是自己需要的超文本就可以了,前面还有一篇也是拼接样式的:Lodo ...

  5. 13点睛Spring4.1-Spring EL

    13.1 Spring EL Spring EL-Spring表达式语言,支持在xml和注解中使用表达式,类似jsp的EL表达式语言; 本教程关注于在注解中使用Spring EL; Spring EL ...

  6. C# sqlsugar依赖引用报错的问题解决

    English Message : You need to refer to MySql.Data.dll↵Chinese Message : 需要引用MySql.Data.dll,请在Nuget安装 ...

  7. Mysql 排序优化

    原文链接:https://www.cnblogs.com/moss_tan_jun/p/6021822.html https://blog.csdn.net/lb517290079/article/d ...

  8. Java后台面试之java基础

    经典类概念性问题 1.java支持的数据类型有哪些?什么是自动拆装箱? 12.Java有哪些特性,举个多态的例子. 14.请列举你所知道的Object类的方法. 15.重载和重写的区别?相同参数不同返 ...

  9. Asp.Net Core中创建多DbContext并迁移到数据库

    在我们的项目中我们有时候需要在我们的项目中创建DbContext,而且这些DbContext之间有明显的界限,比如系统中两个DbContext一个是和整个数据库的权限相关的内容而另外一个DbConte ...

  10. spring boot配置文件application.properties配置JPA以及数据源

    1.application.properties配置jpa模板 spring.datasource.url=jdbc:mysql://localhost:3306/springboottest?use ...