介绍

相对布局管理器是通过相对定位的方式让组件出现在布局的任何位置的。例如如图 3.14 所示的界面就是采用相对布局管理器来进行布局的,其中先放置组件 A,然后放置组件 B,让其位于组件 A 的下方,再放置组件 C,让其位于组件 A 的下方,并且位于组件 B 的右侧。

在Android中,无论是创建哪一种布局管理器都有两种方法:

  1. 一种是在XML布局文件中定义
  2. 另一种是使用Java代码来创建

在下面的语法中,<RelativeLayout> 为起始标记,</RelativeLayout> 为结束标记。在起始标记中的 xmlns:android 为设置 XML 命名空间的属性,其属性值为固定写法。

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
属性列表
>
</RelativeLayout>

支持的XML属性

在相对布局管理器中,只有gravity和ignoreGravity两个属性是不够的,为了更好地控制该布局管理器中各子组件的布局分布,RelativeLayout 提供了一个内部类 RelativeLayout.LayoutParams,通过该类提供的大量 XML 属性,可以很好地控制相对布局管理器中各组件的分布方式。RelativeLayout.LayoutParams 支持的 XML 属性如表所示。

XML属性描述
描 述
android:gravity
用于设置布局管理器中各子组件的对齐方式
android:ignoreGravity
用于指定哪个组件不受 gravity 属性的影响
android:layout_alignBottom / Left
/ Right / Top
其属性值为其他 UI 组件的 id 属性,用于指定该组件与哪个组件的下 / 左 / 右 / 上边界对齐
android:layout_alignParentBottom
/ Left / Right / Top

其属性值为 boolean 值,用于指定该组件是否与布局管理器底端 / 左端 / 右端 / 顶端 对齐
android:layout_above / below /
toLeftOf / toRightOf

其属性值为其他 UI 组件的 id 属性,用于指定该组件位于哪个组件的上方 / 下方 / 左侧 / 右侧
android:layout_centerInParent
其属性值为 boolean 值,用于指定该组件是否位于布局管理器的中央位置
android:layout_centerHorizontal
其属性值为 boolean 值,用于指定该组件是否位于布局管理器水平居中的位置
android:layout_centerVertical
其属性值为 boolean 值,用于指定该组件是否位于布局管理器垂直居中的位置

例子

软件更新提示界面

xml文件代码如下:将提示文本组件textView1设置为在屏幕中央显示,然后设置“以后再说”按钮button2在textView1的下方居右边界对齐,最后设置“现在更新”按钮button1在“以后再说”按钮的左侧显示。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@mipmap/bg"><!-- 添加一个居中显示的文本视图textView1 --><TextView android:text="发现有Widget的新版本,您想现在就安装吗?"android:id="@+id/textView1"android:layout_height="wrap_content"android:layout_width="wrap_content"android:layout_centerInParent="true"/><!-- 添加一个按钮button2,该按钮与textView1的右边界对齐 --><Buttonandroid:text="以后再说"android:id="@+id/button2"android:layout_height="wrap_content"android:layout_width="wrap_content"android:layout_alignRight="@id/textView1"android:layout_below="@id/textView1"/><!-- 添加一个在button2左侧显示的按钮button1 --><Buttonandroid:text="现在更新"android:id="@+id/button1"android:layout_height="wrap_content"android:layout_width="wrap_content"android:layout_below="@id/textView1"android:layout_toLeftOf="@id/button2"/>
</RelativeLayout>

效果图如下:

相对定位属性应用

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:text="Button01"android:textAllCaps="false" /><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_alignParentTop="true"android:text="Button02"android:textAllCaps="false" /><Buttonandroid:id="@+id/button3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="Button03"android:textAllCaps="false" /><Buttonandroid:id="@+id/button4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentBottom="true"android:text="Button04"android:textAllCaps="false" /><Buttonandroid:id="@+id/button5"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_alignParentBottom="true"android:text="Button05"android:textAllCaps="false" /></RelativeLayout>

相对于控件进行定位:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><Buttonandroid:id="@+id/button3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="Button03"android:textAllCaps="false" /><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_above="@+id/button3"android:layout_toLeftOf="@+id/button3"android:text="Button01"android:textAllCaps="false" /><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_above="@+id/button3"android:layout_toRightOf="@+id/button3"android:text="Button02"android:textAllCaps="false" /><Buttonandroid:id="@+id/button4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/button3"android:layout_toLeftOf="@+id/button3"android:text="Button04"android:textAllCaps="false" /><Buttonandroid:id="@+id/button5"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/button3"android:layout_toRightOf="@+id/button3"android:text="Button05"android:textAllCaps="false" /></RelativeLayout>

相对布局管理器RelativeLayout相关推荐

  1. Android中常见五种布局管理器——RelativeLayout、LinearLayout、FrameLayout、TableLayout、GridLayout

    目录 布局管理器 RelativeLayout 常见属性 Relative的实践操作(实现软件更新界面) LinearLayout 常见属性 LinearLayout的实践操作(模范登录以及微信底部) ...

  2. android开发4:Android布局管理器1(线性布局,相对布局RelativeLayout-案例)

    控件类概述 View 可视化控件的基类 属性名称 对应方法 描述 android:background setBackgroundResource(int) 设置背景 android:clickabl ...

  3. 【android编程】 第三讲-Android布局管理器

    android编程 第三讲 Android布局管理器 文章目录 android编程 第三讲 Android布局管理器 约束布局管理器ConstraintLayout 线性布局管理器LinearLayo ...

  4. 【详细】Android入门到放弃篇-YES OR NO-》各种UI组件,布局管理器,单元Activity

    问:达叔,你放弃了吗? 答:不,放弃是不可能的,丢了Android,你会心疼吗?如果别人把你丢掉,你是痛苦呢?还是痛苦呢?~ 引导语 有人说,爱上一个人是痛苦的,有人说,喜欢一个人是幸福的. 人与人之 ...

  5. 【Android 应用开发】AndroidUI设计之 布局管理器 - 详细解析布局实现

    写完博客的总结 : 以前没有弄清楚的概念清晰化 父容器与本容器属性 : android_layout...属性是本容器的属性, 定义在这个布局管理器的LayoutParams内部类中, 每个布局管理器 ...

  6. Android精讲--界面编程2(布局管理器)

    为什么需要布局管理器 为了更好地管理Android应用的用户界面里的各种组件,Android提供了布局管理器.通过使用布局管理器,Android应用的图形用户界面具有良好的平台无关性.通常来说,推荐使 ...

  7. Android学习之布局管理器嵌套

    线性布局管理器 (LinearLayout)里嵌套相对布局管理器(RelativeLayout) 线性布局管理器分为水平布局和垂直布局 水平布局(horizontal):组件从左往右进行排列,所有组件 ...

  8. Android UI详解之布局管理器(一)

    Android UI详解之布局管理器 一.布局管理器 ①顶级父类View ②子类GroupView ③AbsoluteLayout.FrameLayout.LinearLayout.GridLayou ...

  9. 布局管理器的嵌套实现微信朋友圈界面

    布局管理器的嵌套实现微信朋友圈界面 布局管理器嵌套原则: 根布局管理器必须包含xmlns属性 在一个布局文件中,最多只能有一个根布局管理器,如果需要有多个还需要使用一个根布局管理器将他们括起来. 不能 ...

最新文章

  1. 使用ABAP绘制可伸缩矢量图
  2. 左右滑动实现activity之间的跳转
  3. Linux 僵尸进程可以被杀死吗?
  4. axios链接带参数_axios常见传参方式
  5. sublime theme color
  6. 阿里云数据库HybridDB for PostgreSQL使用教程
  7. 在 Linux 中怎样将 MySQL 迁移到 MariaDB 上
  8. 随机变量的原点矩、中心距、变异系数
  9. 网上流行护眼色的RGB值和颜色代码汇总
  10. c语言中的EOF是什么意思
  11. edge浏览器,无法继续下载,提示检测到病毒的问题
  12. Fantastic Graph 2018沈阳网络赛
  13. 钽电解电容跟铝电解电容的区别
  14. 计算机网络ping用法,Ping命令及用法详解
  15. 凡吸纳鲁宾逊微积分者,必须遵守“知识共享”授权许可
  16. SpringMVC笔记
  17. 史上最全软件测试工程师常见的面试题总结(九)【多测师】
  18. 计算机怎么配置IP地址,Windows系统如何给电脑设置IP地址
  19. Nvidia AGX Xavier MAX9286 GMSL 载板(绿板)
  20. Fluent流体材料——NIST Real Gas模型

热门文章

  1. 蚁金服支付宝系统的单元化
  2. 压测工具之Locust
  3. 炫界 (302) -(查动简)_DWG击败G2晋级决赛;简自豪即将英雄归来......
  4. 十一月校园计算机知识竞赛主持词,科技节知识竞赛主持词
  5. python3 类的继承
  6. iPhone12连5G耗电快
  7. vue 返回上一页,页面样式错乱
  8. 波音的冬天,空客的春天
  9. Hexo-Next主题博客个性化配置
  10. 传真机的使用 (发/接收传真) 百度经验