【Android】35、基本布局——RelativeLayout

本篇博文最后修改时间: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,道理都是一样的


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值