Android零基础入门第28节:轻松掌握RelativeLayout相对布局
原文:Android零基础入门第28节:轻松掌握RelativeLayout相对布局
在前面三期中我们对LinearLayout进行了详细的解析,LinearLayout也是我们用的比较多的一个布局。但在实际开发中使用LinearLayout远远不够,我们本期一起来学习RelativeLayout。
一、认识RelativeLayout
RelativeLayout,又叫相对布局,使用RelativeLayout标签。相对布局通常有两种形式,一种是相对于容器而言的,一种是相对于控件而言的。
下表显示了RelativeLayout支持的常用XML属性及相关方法的说明。
为了控制该布局容器中各子组件的布局分布,RelativeLayout提供了一个内部类: RelativeLayout.LayoutParams,该类提供了大量的XML属性来控制RelativeLayout布局容器中子组件的布局分布。
在相对于容器定位的属性主要有以下几个,属性值为true或false。
android:layout_centerHorizontal:控制该组件是否和布局容器的水平居中。
android:layout_centerVertical:控制该组件是否和布局容器的垂直居中。
android:layout_centerInparent:控制该组件是否和布局容器的中央位置。
android:layout_alignParentTop:控制该组件是否和布局容器的顶部对齐。
android:layout_alignParentBottom:控制该组件是否和布局容器的底端对齐。
android:layout_alignParentLeft:控制该组件是否和布局容器的左边对齐。
android:layout_alignParentRight:控制该组件是否和布局容器的右边对齐。
android:layout_alignParentStart:控制该组件是否和布局容器的开始对齐。
android:layout_alignParentEnd:控制该组件是否和布局容器的末端对齐。
android:layout_alignWithParentIfMissing:如果对应的兄弟组件找不到的话就以父容器做参照物。
在相对于其他组件定位的属性主要有以下几个,属性值为其他组件的id。
android:layout_toLeftOf:本组件在某组件的左边。
android:layout_toRightOf:本组件在某组件的右边。
android:layout_toStartOf:本组件在某组件开始端。
android:layout_toEndOf:本组件在某组件末端。
android:layout_above:本组件在某组件的上方。
android:layout_below:本组件在某组件的下方。
android:layout_alignBaseline:本组件和某组件的基线对齐。
android:layout_alignTop:本组件的顶部和某组件的的顶部对齐。
android:layout_alignBottom:本组件的下边缘和某组件的的下边缘对齐。
android:layout_alignRight:本组件的右边缘和某组件的的右边缘对齐。
android:layout_alignLeft:本组件左边缘和某组件左边缘对齐。
android:layout_alignStart:本组件的开始端和某组件开始端对齐。
android:layout_alignEnd:本组件的末端和某组件末端对齐。
除此之外,RelativeLayout.LayoutParams 还继承了 android view. ViewGroup.MarginLayoutParams,因此 RelativeLayout 布局容器中每个子组件也可指定 android.view.ViewGroiip.MarginLayoutParams所支持的各XML属性。
二、示例
接下来通过一个简单的示例程序来学习RelativeLayout的使用用法。
继续使用app/main/res/layout/目录下的activity_main.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:text="容器左上侧"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"/>
<!-- 定义该组件位于父容器上侧水平居中 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="容器上侧水平居中"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
<!-- 定义该组件位于父容器右上侧 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="容器右上侧"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"/>
<!-- 定义该组件位于父容器左侧垂直居中 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="左中"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"/>
<!-- 定义该组件位于父容器右侧垂直居中 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="右中"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"/>
<!-- 定义该组件位于父容器左下侧 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="容器左下侧"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"/>
<!-- 定义该组件位于父容器下侧水平居中 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="容器下侧水平居中"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
<!-- 定义该组件位于父容器右下侧 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="容器右下侧"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"/> <!-- 定义该组件位于父容器中间 -->
<Button
android:id="@+id/center_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="容器中央"
android:layout_centerInParent="true"/>
<!-- 定义该组件位于center_btn组件的上方 -->
<Button
android:id="@+id/center_top_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="中上"
android:layout_above="@id/center_btn"
android:layout_alignLeft="@id/center_btn"/>
<!-- 定义该组件位于center_btn组件的下方 -->
<Button
android:id="@+id/center_bottom_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="中下"
android:layout_below="@id/center_btn"
android:layout_alignLeft="@id/center_btn"/>
<!-- 定义该组件位于center_btn组件的左边 -->
<Button
android:id="@+id/center_bottom_left_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="中下左"
android:layout_toLeftOf="@id/center_btn"
android:layout_alignTop="@id/center_bottom_btn"/>
<!-- 定义该组件位于center_btn组件的右边 -->
<Button
android:id="@+id/center_top_right_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="中上右"
android:layout_toRightOf="@id/center_btn"
android:layout_alignTop="@id/center_top_btn"/>
</RelativeLayout>
运行程序,可以看到下图所示界面效果:
到此,RelativeLayout的示例结束,关于RelativeLayout的更多用法可以参照上面的XML属性和方法参照表,建议多动手练习。
今天就先到这里,如果有问题欢迎留言一起探讨,也欢迎加入Android零基础入门技术讨论微信群,共同成长!
此文章版权为微信公众号分享达人秀(ShareExpert)——鑫鱻所有,若转载请备注出处,特此声明!
往期总结分享:
Android零基础入门第1节:Android的前世今生
Android零基础入门第2节:Android 系统架构和应用组件那些事
Android零基础入门第3节:带你一起来聊一聊Android开发环境
Android零基础入门第4节:正确安装和配置JDK, 高富帅养成第一招
Android零基础入门第5节:善用ADT Bundle, 轻松邂逅女神
Android零基础入门第6节:配置优化SDK Manager, 正式约会女神
Android零基础入门第7节:搞定Android模拟器,开启甜蜜之旅
Android零基础入门第8节:HelloWorld,我的第一趟旅程出发点
Android零基础入门第9节:Android应用实战,不懂代码也可以开发
Android零基础入门第10节:开发IDE大升级,终于迎来了Android Studio
Android零基础入门第11节:简单几步带你飞,运行Android Studio工程
Android零基础入门第12节:熟悉Android Studio界面,开始装逼卖萌
Android零基础入门第13节:Android Studio配置优化,打造开发利器
Android零基础入门第14节:使用高速Genymotion,跨入火箭时代
Android零基础入门第15节:掌握Android Studio项目结构,扬帆起航
Android零基础入门第16节:Android用户界面开发概述
Android零基础入门第17节:TextView属性和方法大全
Android零基础入门第18节:EditText的属性和使用方法
Android零基础入门第19节:Button使用详解
Android零基础入门第20节:CheckBox和RadioButton使用大全
Android零基础入门第21节:ToggleButton和Switch使用大全
Android零基础入门第22节:ImageView的属性和方法大全
Android零基础入门第23节:ImageButton和ZoomButton使用大全
Android零基础入门第24节:自定义View简单使用,打造属于你的控件
Android零基础入门第25节:简单且最常用的LinearLayout线性布局
Android零基础入门第26节:两种对齐方式,layout_gravity和gravity大不同
Android零基础入门第27节:正确使用padding和margin
Android零基础入门第28节:轻松掌握RelativeLayout相对布局的更多相关文章
- Android零基础入门第29节:善用TableLayout表格布局,事半功倍
原文:Android零基础入门第29节:善用TableLayout表格布局,事半功倍 前面学习了线性布局和相对布局,线性布局虽然方便,但如果遇到控件需要排列整齐的情况就很难达到要求,用相对布局又比较麻 ...
- Android零基础入门第70节:ViewPager轻松完成TabHost效果
上一期学习了ViewPager的简单使用,本期一起来学习ViewPager的更多用法. 相信很多同学都使用过今日头条APP吧,一打开主界面就可以看到顶部有很多Tab,然后通过左右滑动来切换,就可以通过 ...
- Android零基础入门第30节:两分钟掌握FrameLayout帧布局
原文:Android零基础入门第30节:两分钟掌握FrameLayout帧布局 前面学习了线性布局.相对布局.表格布局,那么本期来学习第四种布局--FrameLayout帧布局. 一.认识FrameL ...
- Android零基础入门第32节:新推出的GridLayout网格布局
原文:Android零基础入门第32节:新推出的GridLayout网格布局 本期主要学习的是网格布局是Android 4.0新增的布局,和前面所学的TableLayout表格布局 有点类似,不过他有 ...
- Android零基础入门第31节:几乎不用但要了解的AbsoluteLayout绝对布局
原文:Android零基础入门第31节:几乎不用但要了解的AbsoluteLayout绝对布局 前面几期基本学习了Android开发中常用的四种布局,之所以把AbsoluteLayout放在后面来学习 ...
- Android零基础入门第58节:数值选择器NumberPicker
原文:Android零基础入门第58节:数值选择器NumberPicker 上一期学习了日期选择器DatePicker和时间选择器TimePicker,是不是感觉非常简单,本期继续来学习数值选择器Nu ...
- Android零基础入门第59节:AnalogClock、DigitalClock和TextClock时钟组件
原文:Android零基础入门第59节:AnalogClock.DigitalClock和TextClock时钟组件 在前面一期,我们学习了DatePicker和TimePicker,在实际开发中其不 ...
- Android零基础入门第57节:日期选择器DatePicker和时间选择器TimePicker
原文:Android零基础入门第57节:日期选择器DatePicker和时间选择器TimePicker 在实际开发中,经常会遇见一些时间选择器.日期选择器.数字选择器等需求,那么从本期开始来学习And ...
- Android零基础入门第56节:翻转视图ViewFlipper打造引导页和轮播图
原文:Android零基础入门第56节:翻转视图ViewFlipper打造引导页和轮播图 前面两期学习了 ViewAnimator及其子类ViewSwitcher的使用,以及ViewSwitcher的 ...
随机推荐
- 卷积神经网络Lenet-5实现
卷积神经网络Lenet-5实现 原文地址:http://blog.csdn.net/hjimce/article/details/47323463 作者:hjimce 卷积神经网络算法是n年前就有的算 ...
- Android Studio 连接自己搭建的server 须要admin 的帐号的问题 SSH Password Login,please enter password for user git@git.
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/ ...
- js实现去文本换行符小工具
js实现去文本换行符小工具 一.总结 一句话总结: 1.vertical属性使用的时候注意看清定义,也注意父元素的基准线问题.vertical-align:top; 2.获取textareaEleme ...
- Oracle12c导入scott测试用户(转)
登入DBA用户 connect sys as sysdba; 创建scott用户 create user c##scott identified by tiger;--用户名前加c##,12c要求 授 ...
- UIPasteboard粘贴板:UITableView复制粘贴功能(二)
这篇咱写写一写在UITableView上实用剪贴板的两种方法: 一:在自定义cell里面使用 其实,在cell的使用跟在label里面的用法基本一样 1.放方法: - (BOOL)canBecomeF ...
- 【codeforces 791D】 Bear and Tree Jumps
[题目链接]:http://codeforces.com/contest/791/problem/D [题意] 你可以从树上的节点一次最多走k条边. (称为跳一次); 树为无权树; 然后问你任意两点之 ...
- 将字符串转换成xml并取得对应的值
如数据库中有一个字段保存了xml格式的一串字符串: <?xml version="1.0" encoding="utf-16"?><Array ...
- C++利用结构
#include <iostream> using std::cout; using std::endl; //定义结构 struct Box{ double length; double ...
- Win7使用初体验
作者:朱金灿 来源:http://blog.csdn.net/clever101 最近装了个Win7旗舰版.随着年纪渐大,我对软件使用越来越趋于务实,不再追求最新版本,因为我评价一个好软件的标准是适用 ...
- 窗体的基类中没有设定大小,所以才不能居中,若要窗体居中,必须使用setfixedsize()函数或者resize()函数设定窗体的大小,居中才能正常使用
最近开发中,遇到了窗体不能居中的问题,看了网上的很多文章,窗口居中,无非都是move至窗口的中心目标; 有两种方式, 一种在构造函数中直接计算中心坐标; 另一种是在窗口show后再move至相应坐标. ...