【Android】35、基本布局——RelativeLayout
来源:互联网 发布:丁彦雨航体测数据 编辑:程序博客网 时间:2024/06/06 19:39
本篇博文最后修改时间:2016年7月20日,0:27
一、简介
本篇介绍基本布局——RelativeLayout。
二、实验平台
系统版本:Windows7 家庭普通版 32位操作系统。
三、版权声明
博主:思跡
声明:喝水不忘挖井人,转载请注明出处。
原文地址:http://blog.csdn.net/omoiato
联系方式:315878825@qq.com
Java零基础入门交流群:541462902
四、基本布局——RelativeLayout
RelativeLayout 又称作相对布局,也是一种非常常用的布局。
和LinearLayout 的排列规则不同,RelativeLayout 显得更加随意一些,
它可以通过相对定位的方式让控件出现在布局的任何位置。
也正因为如此,RelativeLayout 中的属性非常多,不过这些属性都是有规律可循的,
其实并不难理解和记忆。我们还是通过实践来体会一下。
修改activity_main.xml 中的代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="Button 1" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:text="Button 2" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="Button 3" /> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:text="Button 4" /> <Button android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:text="Button 5" /></RelativeLayout>
我们让Button 1和父布局的左上角对齐,
Button 2 和父布局的右上角对齐,
Button 3 居中显示,
Button 4 和父布局的左下角对齐,
Button 5 和父布局的右下角对齐。
虽然android:layout_alignParentLeft、
android:layout_alignParentTop、
android:layout_alignParentRight、
android:layout_alignParentBottom、
android:layout_centerInParent
这几个属性我们之前都没接触过,可是它们的名字已经完全说明了它们的作用。
上面例子中的每个控件都是相对于父布局进行定位的,
那控件可不可以相对于控件进行定位呢?
当然是可以的,修改activity_main.xml 中的代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="Button 3" /> <Button android: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="Button 1" /> <Button android: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="Button 2" /> <Button android: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="Button 4" /> <Button android: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="Button 5" /></RelativeLayout>
这次的代码稍微复杂一点,不过仍然是有规律可循的。
android:layout_above 属性可以让一个控件位于另一个控件的上方,
需要为这个属性指定相对控件id 的引用,这里我们填入了@id/button3 ,
表示让该控件位于Button 3 的上方。其他的属性也都是相似的,
android:layout_below 表示让一个控件位于另一个控件的下方,
android:layout_toLeftOf 表示让一个控件位于另一个控件的左侧,
android:layout_toRightOf 表示让一个控件位于另一个控件的右侧。
注意,当一个控件去引用另一个控件的id 时,
该控件一定要定义在引用控件的后面,不然会出现找不到id 的情况。
RelativeLayout 中还有另外一组相对于控件进行定位的属性,
android:layout_alignLeft 表示让一个控件的左边缘和另一个控件的左边缘对齐,
android:layout_alignRight 表示让一个控件的右边缘和另一个控件的右边缘对齐,
还有android:layout_alignTop 和android:layout_alignBottom,道理都是一样的。
- 【Android】35、基本布局——RelativeLayout
- Android基本布局-RelativeLayout
- Android布局———RelativeLayout布局(相对布局)
- Android布局之——RelativeLayout
- Android布局——RelativeLayout属性
- android布局(三)——RelativeLayout
- Android相对布局RelativeLayout的基本用法
- Android 基本布局(2)FrameLayout和RelativeLayout
- android四种基本布局之RelativeLayout
- RelativeLayout—相对布局
- 四种基本布局之二——RelativeLayout
- Android【布局管理器】——相对布局RelativeLayout
- RelativeLayout——相对布局
- 一步一步学android之布局管理器——RelativeLayout
- Android核心基础——S02E02_布局演示(LinearLayout, RelativeLayout)
- Android基础入门教程——2.2.2 RelativeLayout(相对布局)
- Android界面资料篇——RelativeLayout相对布局(转)
- Android基础入门教程——2.2.2 RelativeLayout(相对布局)
- H5游戏开发的架构总结(二) 服务器端
- SID是与系统运维
- 获取Http协议的请求行,请求头
- B - Bear and Three Balls(sort)
- MyBatis Generator产生的Example类
- 【Android】35、基本布局——RelativeLayout
- 添加鼠标事件侦听
- JAVA的基本数据类型
- linux下安装phpredis
- javaee之servlet的方法详解
- 使用USRP实现ADS-B的飞机信号获取
- MATLAB sum()函数总结
- 第25天:1、项目章程和项目范围说明书的内容,控制账户,用户故事(属于引导式研讨会),收集需求的工具与技术
- Android 单排上王者系列之RxJava基础篇一