相对布局管理器RelativeLayout
介绍
相对布局管理器是通过相对定位的方式让组件出现在布局的任何位置的。例如如图 3.14 所示的界面就是采用相对布局管理器来进行布局的,其中先放置组件 A,然后放置组件 B,让其位于组件 A 的下方,再放置组件 C,让其位于组件 A 的下方,并且位于组件 B 的右侧。
在Android中,无论是创建哪一种布局管理器都有两种方法:
- 一种是在XML布局文件中定义
- 另一种是使用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相关推荐
- Android中常见五种布局管理器——RelativeLayout、LinearLayout、FrameLayout、TableLayout、GridLayout
目录 布局管理器 RelativeLayout 常见属性 Relative的实践操作(实现软件更新界面) LinearLayout 常见属性 LinearLayout的实践操作(模范登录以及微信底部) ...
- android开发4:Android布局管理器1(线性布局,相对布局RelativeLayout-案例)
控件类概述 View 可视化控件的基类 属性名称 对应方法 描述 android:background setBackgroundResource(int) 设置背景 android:clickabl ...
- 【android编程】 第三讲-Android布局管理器
android编程 第三讲 Android布局管理器 文章目录 android编程 第三讲 Android布局管理器 约束布局管理器ConstraintLayout 线性布局管理器LinearLayo ...
- 【详细】Android入门到放弃篇-YES OR NO-》各种UI组件,布局管理器,单元Activity
问:达叔,你放弃了吗? 答:不,放弃是不可能的,丢了Android,你会心疼吗?如果别人把你丢掉,你是痛苦呢?还是痛苦呢?~ 引导语 有人说,爱上一个人是痛苦的,有人说,喜欢一个人是幸福的. 人与人之 ...
- 【Android 应用开发】AndroidUI设计之 布局管理器 - 详细解析布局实现
写完博客的总结 : 以前没有弄清楚的概念清晰化 父容器与本容器属性 : android_layout...属性是本容器的属性, 定义在这个布局管理器的LayoutParams内部类中, 每个布局管理器 ...
- Android精讲--界面编程2(布局管理器)
为什么需要布局管理器 为了更好地管理Android应用的用户界面里的各种组件,Android提供了布局管理器.通过使用布局管理器,Android应用的图形用户界面具有良好的平台无关性.通常来说,推荐使 ...
- Android学习之布局管理器嵌套
线性布局管理器 (LinearLayout)里嵌套相对布局管理器(RelativeLayout) 线性布局管理器分为水平布局和垂直布局 水平布局(horizontal):组件从左往右进行排列,所有组件 ...
- Android UI详解之布局管理器(一)
Android UI详解之布局管理器 一.布局管理器 ①顶级父类View ②子类GroupView ③AbsoluteLayout.FrameLayout.LinearLayout.GridLayou ...
- 布局管理器的嵌套实现微信朋友圈界面
布局管理器的嵌套实现微信朋友圈界面 布局管理器嵌套原则: 根布局管理器必须包含xmlns属性 在一个布局文件中,最多只能有一个根布局管理器,如果需要有多个还需要使用一个根布局管理器将他们括起来. 不能 ...
最新文章
- 使用ABAP绘制可伸缩矢量图
- 左右滑动实现activity之间的跳转
- Linux 僵尸进程可以被杀死吗?
- axios链接带参数_axios常见传参方式
- sublime theme color
- 阿里云数据库HybridDB for PostgreSQL使用教程
- 在 Linux 中怎样将 MySQL 迁移到 MariaDB 上
- 随机变量的原点矩、中心距、变异系数
- 网上流行护眼色的RGB值和颜色代码汇总
- c语言中的EOF是什么意思
- edge浏览器,无法继续下载,提示检测到病毒的问题
- Fantastic Graph 2018沈阳网络赛
- 钽电解电容跟铝电解电容的区别
- 计算机网络ping用法,Ping命令及用法详解
- 凡吸纳鲁宾逊微积分者,必须遵守“知识共享”授权许可
- SpringMVC笔记
- 史上最全软件测试工程师常见的面试题总结(九)【多测师】
- 计算机怎么配置IP地址,Windows系统如何给电脑设置IP地址
- Nvidia AGX Xavier MAX9286 GMSL 载板(绿板)
- Fluent流体材料——NIST Real Gas模型