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. CentOS7下使用Sentinel实现Redis集群高可用

    Sentinel是Redis官方提供的一种高可用方案(除了Sentinel,Redis Cluster是另一种方案),它可以自动监控Redis master/slave的运行状态,如果发现master ...

  2. 迅速解决!!!!!启动Tomcat报错PermGen space

    启动Tomcat报错   PermGen space    内存溢出 解决方法:扩大tomcat内存 修改参数:set JAVA_OPTS=-Xms1024m -Xmx1024m -XX:PermSi ...

  3. 保存wave音频流的方法

    保存到文件: void saveToFile(String filename, ByteBuffer buffer) { byte[] bytes = buffer.array(); OutputSt ...

  4. RobotFramework: 获取当前时间戳

    robot中先导入DateTime库 #获取当前时间:2019-07-31 10:46:29.940 ${time} Get Current Date result_format=timestamp ...

  5. 预训练中Word2vec,ELMO,GPT与BERT对比

    预训练 先在某个任务(训练集A或者B)进行预先训练,即先在这个任务(训练集A或者B)学习网络参数,然后存起来以备后用.当我们在面临第三个任务时,网络可以采取相同的结构,在较浅的几层,网络参数可以直接加 ...

  6. 用Python打开文件夹

    用Python读取文件夹, 然后打开文件 下面读取到文件的每一个内容, 然后加上路径 import os path = r'../Downloads/text/content' for filenam ...

  7. C++用于类型转换的4个操作符

    Dynamic_cast,   const_cast,  static_cast,  reinterpret_cast. (1)reinterpret_cast 用于基本的类型转换.如 in *ip; ...

  8. java获取当前年、半年、季度、月、日、小时 开始结束时间等

    import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * 获取 当前年.半年 ...

  9. Nginx里的root/index/alias/proxy_pass的意思

    1.[alias] 别名配置,用于访问文件系统,在匹配到location配置的URL路径后,指向[alias]配置的路径.如: location /test/ { alias /home/sftp/i ...

  10. [转帖]UCloud上市:利润暴跌84%、成本居高不下,结构化调整迫在眉睫

    UCloud上市:利润暴跌84%.成本居高不下,结构化调整迫在眉睫 https://www.iyiou.com/p/116317.html     [ 亿欧导读 ] 日前,上交所科创板上市委召开第27 ...