RelativeLayout相对布局

你的名字 2023-06-19 14:29 172阅读 0赞

RelativeLayout又称作相对布局,也是一种非常常用的布局。和 LinearLayout 的排列规则不同,RelativeLayout 显得更加随意一些,它可以通过相对定位的方式让控件出现在布局的任何位置,不过我个人还是喜欢使用线性布局,因为相对布局里面的属性比较多一些,不过这些属性的值也都是有规律的,这篇文章一起探讨下RelativeLayout相对布局。

1.相对于父布局进行定位参数, 参数为true或者false:

  1. android:layout_centerInparent 相对于父元素完全居中
  2. android:layout_alignParentBottom 贴紧父元素的下边缘
  3. android:layout_alignParentLeft 贴紧父元素的左边缘
  4. android:layout_alignParentRight 贴紧父元素的右边缘
  5. android:layout_alignParentTop 贴紧父元素的上边缘
  6. 1
  7. 2
  8. 3
  9. 4
  10. 5

所以下图的效果显示:
这里写图片描述
布局文件的代码如下:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <Button
  6. android:id="@+id/bt1"
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:layout_alignParentLeft="true"
  10. android:layout_alignParentTop="true"
  11. android:text="Button1"/>
  12. <Button
  13. android:id="@+id/bt2"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:layout_alignParentRight="true"
  17. android:layout_alignParentTop="true"
  18. android:text="Button2"/>
  19. <Button
  20. android:id="@+id/bt3"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:layout_alignParentRight="true"
  24. android:layout_alignParentBottom="true"
  25. android:text="Button3"/>
  26. <Button
  27. android:id="@+id/bt4"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:layout_alignParentLeft="true"
  31. android:layout_alignParentBottom="true"
  32. android:text="Button4"/>
  33. <Button
  34. android:id="@+id/bt5"
  35. android:layout_width="wrap_content"
  36. android:layout_height="wrap_content"
  37. android:layout_centerInParent="true"
  38. android:text="Button5"/>
  39. </RelativeLayout>
  40. 1
  41. 2
  42. 3
  43. 4
  44. 5
  45. 6
  46. 7
  47. 8
  48. 9
  49. 10
  50. 11
  51. 12
  52. 13
  53. 14
  54. 15
  55. 16
  56. 17
  57. 18
  58. 19
  59. 20
  60. 21
  61. 22
  62. 23
  63. 24
  64. 25
  65. 26
  66. 27
  67. 28
  68. 29
  69. 30
  70. 31
  71. 32
  72. 33
  73. 34
  74. 35
  75. 36
  76. 37
  77. 38
  78. 39
  79. 40

2.相对于相对于控件进行定位,参数为“@id/id_name”:

  1. android:layout_below 在某元素的下方
  2. android:layout_above 在某元素的的上方
  3. android:layout_toLeftOf 在某元素的左边
  4. android:layout_toRightOf 在某元素的右边
  5. //下面几组用于对原始的边缘,用法可以参考最下面的例子
  6. android:layout_alignTop 本元素的上边缘和某元素的的上边缘对齐
  7. android:layout_alignLeft 本元素的左边缘和某元素的的左边缘对齐
  8. android:layout_alignBottom 本元素的下边缘和某元素的的下边缘对齐
  9. android:layout_alignRight 本元素的右边缘和某元素的的右边缘对齐
  10. 1
  11. 2
  12. 3
  13. 4
  14. 5
  15. 6
  16. 7
  17. 8
  18. 9
  19. 10

所以实现下面的效果图可以使用相对控件参数:
这里写图片描述
代码如下:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <Button
  6. android:id="@+id/bt5"
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:layout_centerInParent="true"
  10. android:text="Button5"/>
  11. <Button
  12. android:id="@+id/bt1"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:layout_toLeftOf="@id/bt5"
  16. android:layout_above="@id/bt5"
  17. android:text="Button1"/>
  18. <Button
  19. android:id="@+id/bt2"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:layout_toRightOf="@id/bt5"
  23. android:layout_above="@id/bt5"
  24. android:text="Button2"/>
  25. <Button
  26. android:id="@+id/bt3"
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:layout_toLeftOf="@id/bt5"
  30. android:layout_below="@id/bt5"
  31. android:text="Button3"/>
  32. <Button
  33. android:id="@+id/bt4"
  34. android:layout_width="wrap_content"
  35. android:layout_height="wrap_content"
  36. android:layout_toRightOf="@id/bt5"
  37. android:layout_below="@id/bt5"
  38. android:text="Button4"/>
  39. </RelativeLayout>
  40. 1
  41. 2
  42. 3
  43. 4
  44. 5
  45. 6
  46. 7
  47. 8
  48. 9
  49. 10
  50. 11
  51. 12
  52. 13
  53. 14
  54. 15
  55. 16
  56. 17
  57. 18
  58. 19
  59. 20
  60. 21
  61. 22
  62. 23
  63. 24
  64. 25
  65. 26
  66. 27
  67. 28
  68. 29
  69. 30
  70. 31
  71. 32
  72. 33
  73. 34
  74. 35
  75. 36
  76. 37
  77. 38
  78. 39

以上讲完了RelativeLayout中的元素的一些属性,其实是有很大规律的,下面我们来看一个用相对布局完全线性布局中展示如下的一个例子:
这里写图片描述
这里用到了android:layout_alignBottom元素,这也是对齐下边缘必须要用到的,这里直接给出代码:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent">
  2. <Button android:id="@+id/bt1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button1"/>
  3. <EditText android:id="@+id/et1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toRightOf="@id/bt1" android:layout_alignBottom ="@id/bt1" android:hint="TextView1" android:singleLine="true"/>
  4. <Button android:id="@+id/bt2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/bt1" android:text="Button2"/>
  5. <EditText android:id="@+id/et2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toRightOf="@id/bt2" android:layout_alignBottom ="@id/bt2" android:hint="TextView2" android:singleLine="true"/>
  6. </RelativeLayout>
  7. 1
  8. 2
  9. 3
  10. 4
  11. 5
  12. 6
  13. 7
  14. 8
  15. 9
  16. 10
  17. 11
  18. 12
  19. 13
  20. 14
  21. 15
  22. 16
  23. 17
  24. 18
  25. 19
  26. 20
  27. 21
  28. 22
  29. 23
  30. 24
  31. 25
  32. 26
  33. 27
  34. 28
  35. 29
  36. 30
  37. 31
  38. 32
  39. 33

发表评论

表情:
评论列表 (有 0 条评论,172人围观)

还没有评论,来说两句吧...

相关阅读