Android-相对布局(RelativeLayout)

本文详细介绍了Android中的相对布局(RelativeLayout)的使用,包括两种定位方式:相对于父容器和相对于控件。列举了关键属性如`android:layout_centerInparent`、`android:layout_alignParentTop`等,并通过实例展示了如何根据组件ID设置位置关系,创建了一个包含多个Button的布局示例。

开发板推荐:天空星STM32F407VET6开发板

超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

    相对布局通常有两种形式,一种是对于容器而言,另一种是对于控件而言点的。

RelativeLayout用到的一些重要的属性:

一、相对于父容器而言的属性

android:layout_centerInparent          相对于父容器完全居中

android:layout_alignParentTop         贴紧父容器的上边

android:layout_alignParentBottom   贴紧父容器的下边

android:layout_alignParentLeft         贴紧父容器的左边

android:layout_alignParentRight      贴紧容器的右边

二、相对于控件而言的属性

android:layout_below            在某个组件的下边

android:layout_above            在某个组件的上方

android:layout_toLeftOf         在某个组件的左边

android:layout_toRightOf      在某组件的右边

android:layout_alignTop       个组件之间是顶部对齐

android:layout_alignBottom 两个组件之间是底部对齐

android:layout_alignLeft       两个组件之间是左边缘对齐

android:layout_alignRight    两个组件之间是右边缘对齐

三、属性值为具体的像素值

android:layout_marginTop 离某元素上边缘的距离

android:layout_marginBottom 离某元素底边缘的距离

android:layout_marginLeft 离某元素左边缘的距离

android:layout_marginRight 离某元素右边缘的距离

例子:


1、第一步先放置中间的Button3

其他的都相对于中间的Button3定位,指定的是和Button3之间的位置关系,所以要新增一个属性唯一的来标志Button3这个组件android:id="@+id/button3"

[html]  view plain  copy
  1. <Button    
  2.       android:id="@+id/button3"    
  3.       android:layout_width="wrap_content"    
  4.       android:layout_height="wrap_content"    
  5.       android:text="Button3"
  6.      android:layout_contentInParent="true" 
  7. />   
2、设置Button1这个组件,它在Button3的左上方

  1. <Button       
  2.       android:layout_width="wrap_content"    
  3.       android:layout_height="wrap_content"    
  4.       android:text="Button1"
  5.       android:layout_above="@id/button3"    //根据id找到button3,它在button3的上方
  6.       android:layout_toLeftOf="@id/button3"  //在button3的左边
  7. />   

3、设置Button2这个组件,它在Button3的右上方

  1. <Button       
  2.       android:layout_width="wrap_content"    
  3.       android:layout_height="wrap_content"    
  4.       android:text="Button2"
  5.       android:layout_above="@id/button3"    
  6.       android:layout_toReightOf="@id/button3"  
  7. />   
4、设置Button4这个组件,它在Button3的左下方

  1. <Button       
  2.       android:layout_width="wrap_content"    
  3.       android:layout_height="wrap_content"    
  4.       android:text="Button4"
  5.       android:layout_bellow="@id/button3"    
  6.       android:layout_toLeftOf="@id/button3"  
  7. />   
5、设置Button5这个组件,它在Button3的右下方

  1. <Button       
  2.       android:layout_width="wrap_content"    
  3.       android:layout_height="wrap_content"    
  4.       android:text="Button5"
  5.       android:layout_bellow="@id/button3"    
  6.       android:layout_toReightOf="@id/button3"  
  7. />   
完整代码:

  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <RelativeLayout                                     //网络布局管理器  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"    
  4.     android:layout_width="wrap_content"    
  5.     android:layout_height="wrap_content"   />
  6. <Button
  7.     android:id="@+id/button3"
  8.     android:layout_weight="wrap_content"
  9.     android:layout_height="wrap_content" 
  10.     android:text="Button3"
  11.     android:layout_contentInParent="true" 
  12. />
  13. <Button
  14.     android:layout_weight="wrap_content"
  15.     android:layout_height="wrap_content"
  16.     android:text="Button1"
  17.     android:layout_above="@id/button3"
  18.     android:layout_toLeftOf="@id/button3"
  19. />
  20. <Button 
  21.     android:layout_weight="wrap_content"
  22.     android:layout_height="wrap_content"
  23.     android:text="Button2"
  24.     android:layout_above="@id/button3"    
  25.     android:layout_toReightOf="@id/button3"  
  26. />   
  27. <Button       
  28.     android:layout_width="wrap_content"    
  29.     android:layout_height="wrap_content"    
  30.     android:text="Button4"
  31.     android:layout_bellow="@id/button3"    
  32.     android:layout_toLeftOf="@id/button3"  
  33. />  
  34. <Button       
  35.     android:layout_width="wrap_content"    
  36.     android:layout_height="wrap_content"    
  37.     android:text="Button5"
  38.     android:layout_bellow="@id/button3"    
  39.     android:layout_toReightOf="@id/button3"  
  40. />  
  41. </RelativeLayout >

开发板推荐:天空星STM32F407VET6开发板

超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值