Android 相对布局RelativeLayout

ゝ一纸荒年。 2022-06-02 06:22 373阅读 0赞

RelativeLayout下级视图的位置可以用与该视图自身平级的视图或者该视图的上级视图的相对位置来描述。如果没有指定参照视图,那么下级视图默认显示在RelativeLayout内部的左上角。相对位置的属性和类型值参照下表:
这里写图片描述

为了更好的理解上面相对属性的含义,我们使用RelativeLayout来进行一个简单的演示。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context="com.easygoings.androidrelativelayout.MainActivity">
  8. <Button
  9. android:id="@+id/bt_center"
  10. android:layout_width="100dp"
  11. android:layout_height="80dp"
  12. android:layout_centerInParent="true"
  13. android:text="我在父控件中间"/>
  14. <Button
  15. android:id="@+id/bt_center_hor"
  16. android:layout_width="100dp"
  17. android:layout_height="80dp"
  18. android:layout_centerHorizontal="true"
  19. android:text="我在水平中间"/>
  20. <Button
  21. android:id="@+id/bt_center_ver"
  22. android:layout_width="100dp"
  23. android:layout_height="80dp"
  24. android:layout_centerVertical="true"
  25. android:text="我在垂直中间"/>
  26. <Button
  27. android:id="@+id/bt_par_left"
  28. android:layout_width="100dp"
  29. android:layout_height="80dp"
  30. android:layout_marginTop="100dp"
  31. android:layout_alignParentLeft="true"
  32. android:text="上级左边对齐"/>
  33. <Button
  34. android:id="@+id/bt_par_right"
  35. android:layout_width="100dp"
  36. android:layout_height="80dp"
  37. android:layout_marginTop="100dp"
  38. android:layout_alignParentRight="true"
  39. android:text="上级右边对齐"/>
  40. <Button
  41. android:id="@+id/bt_par_top"
  42. android:layout_width="100dp"
  43. android:layout_height="80dp"
  44. android:layout_alignParentTop="true"
  45. android:text="上级顶部对齐"/>
  46. <Button
  47. android:id="@+id/bt_par_bottom"
  48. android:layout_width="100dp"
  49. android:layout_height="80dp"
  50. android:layout_centerHorizontal="true"
  51. android:layout_alignParentBottom="true"
  52. android:text="上级底部对齐"/>
  53. <Button
  54. android:id="@+id/bt_bottom_left"
  55. android:layout_width="100dp"
  56. android:layout_height="80dp"
  57. android:layout_toLeftOf="@+id/bt_par_bottom"
  58. android:layout_alignTop="@+id/bt_par_bottom"
  59. android:text="底部左边"/>
  60. <Button
  61. android:id="@+id/bt_bottom_right"
  62. android:layout_width="100dp"
  63. android:layout_height="80dp"
  64. android:layout_toRightOf="@+id/bt_par_bottom"
  65. android:layout_alignTop="@+id/bt_par_bottom"
  66. android:text="底部右边"/>
  67. <Button
  68. android:id="@+id/bt_center_above"
  69. android:layout_width="100dp"
  70. android:layout_height="80dp"
  71. android:layout_above="@+id/bt_center"
  72. android:layout_alignLeft="@+id/bt_center"
  73. android:text="中间上边"/>
  74. <Button
  75. android:id="@+id/bt_center_bellow"
  76. android:layout_width="100dp"
  77. android:layout_height="80dp"
  78. android:layout_below="@+id/bt_center"
  79. android:layout_alignLeft="@+id/bt_center"
  80. android:text="中间下边"/>
  81. </RelativeLayout>

这里写图片描述

当然了,也可以再代码中动态添加视图,那么相对位置也只能再代码中定义了。在代码中定义相对位置使用的是 RelativeLayout.LayoutParams的addRule方法

  1. @Override
  2. public void onClick(View v) {
  3. switch (v.getId())
  4. {
  5. case R.id.bt_add_left:
  6. addNewView(RelativeLayout.ALIGN_PARENT_LEFT,0,content.getId());
  7. break;
  8. case R.id.bt_add_center:
  9. addNewView(RelativeLayout.CENTER_IN_PARENT,0,content.getId());
  10. break;
  11. case R.id.bt_add_right:
  12. addNewView(RelativeLayout.ALIGN_PARENT_RIGHT,0,content.getId());
  13. break;
  14. }
  15. }
  16. private void addNewView(int firstAlign,int secondAlign,int refid)
  17. {
  18. View v = new View(this);
  19. v.setBackgroundColor(0xaa66dd66);
  20. RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100,80);
  21. params.topMargin = 200;
  22. params.addRule(firstAlign,refid);
  23. if(secondAlign > 0 )
  24. {
  25. params.addRule(secondAlign,refid);
  26. }
  27. v.setLayoutParams(params);
  28. v.setOnLongClickListener(new View.OnLongClickListener()
  29. {
  30. @Override
  31. public boolean onLongClick(View vv) {
  32. content.removeView(vv);
  33. return true;
  34. }
  35. });
  36. content.addView(v);
  37. }

这里写图片描述

发表评论

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

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

相关阅读