1. 线性布局

  1. <LinearLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="horizontal"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. </LinearLayout>

1. android:orientation

指定线性排列方向,可选值有verticalhorizontal

2. android:layout_gravity

指定控件在布局中的对齐方式。

3. android:layout_weight

允许我们使用比例的方式来指定控件的大小。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="horizontal"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <EditText
  7. android:id="@+id/input_mesage"
  8. android:layout_width="0dp"
  9. android:layout_height="wrap_content"
  10. android:layout_weight="1"
  11. android:hint="Type Something">
  12. </EditText>
  13. <Button
  14. android:id="@+id/button1"
  15. android:layout_width="0dp"
  16. android:layout_height="wrap_content"
  17. android:layout_weight="2"
  18. android:text="Send">
  19. </Button>
  20. </LinearLayout>

1.png
类似于前端中的flex布局,通过比例分配控件的占用空间。

在指定宽度或者高度为0dp时,需要指定layout_weight。

2. 相对布局

RelativeLayout又称作为相对布局,是一种非常常用的布局。

1. 相对父布局

  1. <!--相对布局-->
  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. <Button
  7. android:id="@+id/button1"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:layout_alignParentLeft="true"
  11. android:layout_alignParentTop="true"
  12. android:text="button1"
  13. ></Button>
  14. <!--右上角对齐-->
  15. <Button
  16. android:id="@+id/button2"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:layout_alignParentRight="true"
  20. android:layout_alignParentTop="true"
  21. android:text="button2"
  22. ></Button>
  23. <!--居中对齐-->
  24. <Button
  25. android:id="@+id/button3"
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:layout_centerInParent="true"
  29. android:text="button3"
  30. ></Button>
  31. <!--左下角对齐-->
  32. <Button
  33. android:id="@+id/button4"
  34. android:layout_width="wrap_content"
  35. android:layout_height="wrap_content"
  36. android:layout_alignParentBottom="true"
  37. android:layout_alignParentLeft="true"
  38. android:text="button4"
  39. ></Button>
  40. <!--右下角对齐-->
  41. <Button
  42. android:id="@+id/button5"
  43. android:layout_width="wrap_content"
  44. android:layout_height="wrap_content"
  45. android:layout_alignParentBottom="true"
  46. android:layout_alignParentRight="true"
  47. android:text="button5"
  48. ></Button>
  49. </RelativeLayout>

22.png

2. 相对控件

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent">
  4. <Button
  5. android:id="@+id/button1"
  6. android:layout_width="wrap_content"
  7. android:layout_height="wrap_content"
  8. android:layout_above="@+id/button3"
  9. android:layout_toLeftOf="@+id/button3"
  10. android:text="button1"
  11. ></Button>
  12. <Button
  13. android:id="@+id/button2"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:layout_above="@+id/button3"
  17. android:layout_toRightOf="@+id/button3"
  18. android:text="button2"
  19. ></Button>
  20. <Button
  21. android:id="@+id/button3"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:layout_centerInParent="true"
  25. android:text="button3"
  26. ></Button>
  27. <Button
  28. android:id="@+id/button4"
  29. android:layout_width="wrap_content"
  30. android:layout_height="wrap_content"
  31. android:layout_below="@+id/button3"
  32. android:layout_toLeftOf="@+id/button3"
  33. android:text="button4"
  34. ></Button>
  35. <Button
  36. android:id="@+id/button5"
  37. android:layout_width="wrap_content"
  38. android:layout_height="wrap_content"
  39. android:layout_below="@+id/button3"
  40. android:layout_toRightOf="@+id/button3"
  41. android:text="button5"
  42. ></Button>
  43. </RelativeLayout>

33.png

3. 帧布局

FrameLayout又称作帧布局,这种布局没有方便的定位方式,所有控件都会默认摆放在布局的左上角。

4. 百分比布局

AndroidX迁移——弃用support库指南

Android开发笔记:布局控件(四) 百分比布局