一、相对于父容器

1.居中

2.同方向

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5.  
  6. <!--相对于父容器
  7. 1.居中
  8. 2.同方向对齐方式
  9. -->
  10. <Button
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="按钮一"
  14. android:layout_centerInParent="true"
  15. /><!--位于父容器居中位置-->
  16. <Button
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:text="按钮二"
  20. android:layout_alignParentBottom="true"
  21. android:layout_centerHorizontal="true"
  22. /><!--与父容器底端对齐-->
  23. <!--位于父容器水平居中位置-->
  24. <Button
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:text="按钮三"
  28. android:layout_alignParentRight="true"
  29. android:layout_centerVertical="true"
  30. /><!--与父容器右侧对齐-->
  31. <!--位于父容器垂直居中位置-->
  32. <Button
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:text="按钮四"
  36. android:layout_alignParentLeft="true"
  37. android:layout_centerVertical="true"
  38. /><!--与父容器左侧对齐-->
  39. <!--位于父容器垂直居中位置-->
  40. <Button
  41. android:layout_width="wrap_content"
  42. android:layout_height="wrap_content"
  43. android:text="按钮五"
  44. android:layout_alignParentTop="true"
  45. android:layout_centerHorizontal="true"
  46. /><!--与父容器顶端对齐-->
  47. <!--位于父容器水平居中位置-->
  48. <Button
  49. android:layout_width="wrap_content"
  50. android:layout_height="wrap_content"
  51. android:text="按钮六"
  52. /><!--与父容器左侧对齐-->
  53. <!--与父容器顶端对齐-->
  54. <!--默认位置-->
  55. <Button
  56. android:layout_width="wrap_content"
  57. android:layout_height="wrap_content"
  58. android:text="按钮七"
  59. android:layout_alignParentRight="true"
  60. /><!--与父容器右侧对齐-->
  61. <!--与父容器顶端对齐-->
  62. <Button
  63. android:layout_width="wrap_content"
  64. android:layout_height="wrap_content"
  65. android:text="按钮八"
  66. android:layout_alignParentLeft="true"
  67. android:layout_alignParentBottom="true"
  68. /><!--与父容器左侧对齐-->
  69. <!--与父容器底端对齐-->
  70. <Button
  71. android:layout_width="wrap_content"
  72. android:layout_height="wrap_content"
  73. android:text="按钮十"
  74. android:layout_alignParentRight="true"
  75. android:layout_alignParentBottom="true"
  76. /><!--与父容器右侧对齐-->
  77. <!--与父容器底端对齐-->
  78. </RelativeLayout>

二、与兄弟组件的相对位置

1.同方向

2.反方向

+lay_outmargin +padding

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <!--与兄弟组件之间
  6. 1.同方向
  7. 2.反方向-->
  8.  
  9. <!--margin 外边距
  10. padding 内间距
  11. -->
  12. <Button
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:text="按钮一"
  16. android:layout_centerInParent="true"
  17. android:id="@+id/bt"/><!--父容器内居中-->
  18. <Button
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:text="按钮二"
  22. android:layout_alignTop="@id/bt"
  23. android:layout_toLeftOf="@id/bt"/>
  24. //相对于按钮一 顶部对齐 位于左侧
  25.  
  26. <Button
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:text="按钮三"
  30. android:layout_alignRight="@id/bt"
  31. android:layout_above="@id/bt"/>
  32. //相对于按钮一 右侧对齐 位于上方
  33. <Button
  34. android:layout_width="wrap_content"
  35. android:layout_height="wrap_content"
  36. android:text="按钮四"
  37. android:layout_alignRight="@id/bt"
  38. android:layout_below="@id/bt"/>
  39. //相对于按钮一 右侧对齐 位于下方
  40. <Button
  41. android:layout_width="wrap_content"
  42. android:layout_height="wrap_content"
  43. android:text="按钮五"
  44. android:layout_alignTop="@id/bt"
  45. android:layout_toRightOf="@id/bt"/>
  46. //相对于按钮一 顶部对齐 位于右侧
  47. <Button
  48. android:layout_width="wrap_content"
  49. android:layout_height="wrap_content"
  50. android:text="按钮六"
  51. android:layout_toRightOf="@id/bt"
  52. android:layout_below="@id/bt"/>
  53. //相对于按钮一 位于右侧 位于下方
  54. <Button
  55. android:layout_width="wrap_content"
  56. android:layout_height="wrap_content"
  57. android:text="按钮七"
  58. android:layout_toRightOf="@id/bt"
  59. android:layout_above="@id/bt"/>
  60. //相对于按钮一 位于右侧 位于上方
  61.  
  62. <Button
  63. android:layout_width="wrap_content"
  64. android:layout_height="wrap_content"
  65. android:text="按钮八"
  66. android:layout_toLeftOf="@id/bt"
  67. android:layout_above="@id/bt"/>
  68. //相对于按钮一 位于左侧 位于上方
  69.  
  70. <Button
  71. android:layout_width="wrap_content"
  72. android:layout_height="wrap_content"
  73. android:text="按钮九"
  74. android:layout_toLeftOf="@id/bt"
  75. android:layout_below="@id/bt"/>
  76. //相对于按钮一 位于左侧 位于下方
  77. <EditText
  78. android:layout_width="match_parent"
  79. android:layout_height="wrap_content"
  80. android:hint="输入框"
  81. android:id="@+id/et"
  82. android:paddingLeft="20dp"
  83. android:layout_marginTop="20dp"
  84. android:layout_marginBottom="10dp"/>
  85. //内左侧间距20dp 上边距20dp 下边距10dp
  86.  
  87. <Button
  88. android:layout_width="wrap_content"
  89. android:layout_height="wrap_content"
  90. android:text="OK"
  91. android:layout_alignParentRight="true"
  92. android:layout_below="@id/et"
  93. android:id="@+id/ok"/>
  94. //ok按钮 位于父窗口右侧 输入框下方
  95. <Button
  96. android:layout_width="wrap_content"
  97. android:layout_height="wrap_content"
  98. android:text="CANCLE"
  99. android:layout_alignTop="@id/ok"
  100. android:layout_toLeftOf="@id/ok"
  101. android:layout_marginRight="20dp"/>
  102. //cancle按钮 相对于ok按钮顶部对齐 位于左侧 右边距20dp
  103.  
  104. </RelativeLayout>

Android——RelativeLayout(相对布局)的更多相关文章

  1. Android RelativeLayout相对布局

    RelativeLayout是相对布局控件:以控件之间相对位置或相对父容器位置进行排列. 相对布局常用属性: 子类控件相对子类控件:值是另外一个控件的id android:layout_above-- ...

  2. Android开发重点难点1:RelativeLayout(相对布局)详解

    前言 啦啦啦~博主又推出了一个新的系列啦~ 之前的Android开发系列主要以完成实验的过程为主,经常会综合许多知识来写,所以难免会有知识点的交杂,给人一种混乱的感觉. 所以博主推出“重点难点”系列, ...

  3. Android开发3:Intent、Bundle的使用和ListView的应用 、RelativeLayout(相对布局)简述(简单通讯录的实现)

    前言 啦啦啦~博主又来骚扰大家啦~大家是不是感觉上次的Android开发博文有点长呢~主要是因为博主也是小白,在做实验的过程中查询了很多很多概念,努力去理解每一个知识点,才完成了最终的实验.还有就是随 ...

  4. Android(java)学习笔记164:Relativelayout相对布局案例

    我们看看案例代码,自己心领神会: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout ...

  5. 第13章、布局Layouts之RelativeLayout相对布局(从零開始学Android)

    RelativeLayout相对布局 RelativeLayout是一种相对布局,控件的位置是依照相对位置来计算的,后一个控件在什么位置依赖于前一个控件的基本位置,是布局最经常使用,也是最灵活的一种布 ...

  6. Android开发之布局--RelativeLayout布局

    RelativeLayout 相对布局 true或false属性 Layout_centerHorizontal   当控件位于父控件的横向中间位置 Layout_centerVertical   当 ...

  7. Android精通:TableLayout布局,GridLayout网格布局,FrameLayout帧布局,AbsoluteLayout绝对布局,RelativeLayout相对布局

    在Android中提供了几个常用布局: LinearLayout线性布局 RelativeLayout相对布局 FrameLayout帧布局 AbsoluteLayout绝对布局 TableLayou ...

  8. Android精通:View与ViewGroup,LinearLayout线性布局,RelativeLayout相对布局,ListView列表组件

    UI的描述 对于Android应用程序中,所有用户界面元素都是由View和ViewGroup对象构建的.View是绘制在屏幕上能与用户进行交互的一个对象.而对于ViewGroup来说,则是一个用于存放 ...

  9. Android(java)学习笔记107:Relativelayout相对布局

    1. Relativelayout相对布局案例: 我们看看案例代码,自己心领神会: <?xml version="1.0" encoding="utf-8" ...

随机推荐

  1. 转:Eclipse ADT的Custom debug keystore所需证书规格

    转:http://blog.k-res.net/archives/1229.html Eclipse ADT的Custom debug keystore所需证书规格 三月 8, 2013  |  Po ...

  2. Ubuntu简单搭建git私有服务

    gitserver搭建过程 搭建gitserver过程记录 例如以下: 环境: serverUbuntu虚拟机(Boss),能通过网络訪问到(server地址:192.168.9.103). clie ...

  3. Exdata cell 节点配置时遇到的一个问题

    问题描写叙述: [celladmin@vrh4 ~]$ cellcli CellCLI: Release 11.2.3.2.0 - Production on Sat Jun 14 09:11:08 ...

  4. 在centOS上安装VNC

    步骤如下: 1.搜寻VNC Server [root@msg45 wasliberty]# yum search tigervnc-serverLoaded plugins: fastestmirro ...

  5. ThinkPHP框架返回插入记录的id号

    ThinkPHP返回插入记录的id号 $Form->create()) $result = $Form->add(); 在执行上述语句后,若存在auto_increment字段,则可以使用 ...

  6. ant design pro (十四)advanced 使用 CLI 工具

    一.概述 原文地址:https://pro.ant.design/docs/cli-cn 为了更好以及高效的开发效率,我们提供了配套的 ant-design-pro-cli 工具. pro cli 提 ...

  7. LeetCode 110 Balanced Binary Tree(平衡二叉树)(*)

    翻译 给定一个二叉树,决定它是否是高度平衡的. (高度是名词不是形容词-- 对于这个问题.一个高度平衡二叉树被定义为: 这棵树的每一个节点的两个子树的深度差不能超过1. 原文 Given a bina ...

  8. 2、JSP脚本

    JSP脚本 JSP脚本包含了JSP表达式.声明标识和脚本程序.通过这些标识,在JSP页面中可以如同编写Java程序一样来声明变量.定义方法和执行各种表达式的运算 1.在JSP中应用代码片段 语法格式: ...

  9. 注意Hibernate4在开发当中的一些改变

    Hibernate4的改动较大只有spring3.1以上版本能够支持,Spring3.1取消了HibernateTemplate,因为Hibernate4的事务管理已经很好了,不用Spring再扩展了 ...

  10. 引用、数组引用与指针引用、内联函数inline、四种类型转换运算符

    一.引用 (1).引用是给一个变量起别名 定义引用的一般格式:类型  &引用名 = 变量名: 例如:int a=1;  int  &b=a;// b是a的别名,因此a和b是同一个单元 ...