基础复习——线性布局LinearLayout——相对布局RelativeLayout——网格布局GridLayout——滚动视图ScrollView...

约定不等于承诺〃 2023-09-28 17:38 109阅读 0赞

线性布局LinearLayout

线性布局内部的各视图有两种排列方式:

(1)orientation属性值为horizontal时,内部视图在水平方向从左往右排列。

(2)orientation属性值为vertical时,内部视图在垂直方向从上往下排列。

如果不指定orientation属性,则LinearLayout默认水平方向排列。

布局:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical">
  5. <LinearLayout
  6. android:layout_width="match_parent"
  7. android:layout_height="wrap_content"
  8. android:background="#ffff99"
  9. android:orientation="horizontal">
  10. <TextView
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="横排第一个"
  14. android:textSize="17sp"
  15. android:textColor="#000000" />
  16. <TextView
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:text="横排第二个"
  20. android:textSize="17sp"
  21. android:background="#00ffff"
  22. android:textColor="#000000" />
  23. </LinearLayout>
  24. <LinearLayout
  25. android:layout_width="match_parent"
  26. android:layout_height="wrap_content"
  27. android:background="#ff0000"
  28. android:orientation="vertical">
  29. <TextView
  30. android:layout_width="wrap_content"
  31. android:layout_height="wrap_content"
  32. android:text="竖排第一个"
  33. android:textSize="17sp"
  34. android:textColor="#000000" />
  35. <TextView
  36. android:layout_width="wrap_content"
  37. android:layout_height="wrap_content"
  38. android:text="竖排第二个"
  39. android:textSize="17sp"
  40. android:background="#00ffff"
  41. android:textColor="#000000" />
  42. </LinearLayout>
  43. </LinearLayout>

6767450c985d284dc11369ee26bee1eb.png

e6db96c335a1afed6eba93a641ccd54c.png

90e8c22a3a7daf9b1851352f36a54730.png

bd2a2879af66c55fe0813c3ff9a9f2d9.png

759f9937a5d54eb0d469c431b2dac865.png

线性布局的权重概念,指的是线性布局的下级视图各自拥有多大比例的宽高。

权重属性名叫layout_weight,但该属性不在LinearLayout节点设置,而在线性布局的直接下级视图设置,表示该下级视图占据的宽高比例。

(1)layout_width填0dp时,layout_weight表示水平方向的宽度比例。

(2)layout_height填0dp时,layout_weight表示垂直方向的高度比例。

d9b00319af8e19ad29821af5f013cf57.png

6c5a07fc5cf256deb6e9f6714a6f6dc3.png

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical">
  5. <LinearLayout
  6. android:layout_width="match_parent"
  7. android:layout_height="wrap_content"
  8. android:background="#ff0000"
  9. android:orientation="horizontal">
  10. <TextView
  11. android:layout_width="0dp"
  12. android:layout_height="wrap_content"
  13. android:layout_weight="1"
  14. android:gravity="center"
  15. android:text="横排第一个"
  16. android:textSize="17sp"
  17. android:textColor="#000000" />
  18. <TextView
  19. android:layout_width="0dp"
  20. android:layout_height="wrap_content"
  21. android:layout_weight="1"
  22. android:gravity="center"
  23. android:text="横排第二个"
  24. android:textSize="17sp"
  25. android:textColor="#000000" />
  26. </LinearLayout>
  27. <LinearLayout
  28. android:layout_width="match_parent"
  29. android:layout_height="100dp"
  30. android:background="#00ffff"
  31. android:orientation="vertical">
  32. <TextView
  33. android:layout_width="match_parent"
  34. android:layout_height="0dp"
  35. android:layout_weight="1"
  36. android:gravity="center"
  37. android:text="竖排第一个"
  38. android:textSize="17sp"
  39. android:textColor="#000000" />
  40. <TextView
  41. android:layout_width="match_parent"
  42. android:layout_height="0dp"
  43. android:layout_weight="1"
  44. android:gravity="center"
  45. android:text="竖排第二个"
  46. android:textSize="17sp"
  47. android:textColor="#000000" />
  48. </LinearLayout>
  49. </LinearLayout>

32431215d76b60e95a35678af572cdad.png

ee2d01eb941f79f3f0b30a8456c92ec7.png

b974200f857f21501a85b2b8d2c18704.png

a9bf49c244cebea4c9b71559bf293465.png

=====================================================================================================

相对布局RelativeLayout

相对布局的下级视图位置由其他视图决定。用于确定下级视图位置的参照物分两种:

(1)与该视图自身平级的视图;

(2)该视图的上级视图(也就是它归属的RelativeLayout)

如果不设定下级视图的参照物,那么下级视图默认显示在RelativeLayout内部的左上角。

下面是相对位置的取值说明:

80425924a1a5694c442bd361db3d1eba.png

布局:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="150dp" >
  4. <TextView
  5. android:id="@+id/tv_center"
  6. android:layout_width="wrap_content"
  7. android:layout_height="wrap_content"
  8. android:layout_centerInParent="true"
  9. android:background="#ffffff"
  10. android:text="我在中间"
  11. android:textSize="11sp"
  12. android:textColor="#000000" />
  13. <TextView
  14. android:id="@+id/tv_center_horizontal"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:layout_centerHorizontal="true"
  18. android:background="#eeeeee"
  19. android:text="我在水平中间"
  20. android:textSize="11sp"
  21. android:textColor="#000000" />
  22. <TextView
  23. android:id="@+id/tv_center_vertical"
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:layout_centerVertical="true"
  27. android:background="#eeeeee"
  28. android:text="我在垂直中间"
  29. android:textSize="11sp"
  30. android:textColor="#000000" />
  31. <TextView
  32. android:id="@+id/tv_parent_left"
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:layout_alignParentLeft="true"
  36. android:background="#eeeeee"
  37. android:text="我跟上级左边对齐"
  38. android:textSize="11sp"
  39. android:textColor="#000000" />
  40. <TextView
  41. android:id="@+id/tv_parent_right"
  42. android:layout_width="wrap_content"
  43. android:layout_height="wrap_content"
  44. android:layout_alignParentRight="true"
  45. android:background="#eeeeee"
  46. android:text="我跟上级右边对齐"
  47. android:textSize="11sp"
  48. android:textColor="#000000" />
  49. <TextView
  50. android:id="@+id/tv_parent_top"
  51. android:layout_width="wrap_content"
  52. android:layout_height="wrap_content"
  53. android:layout_alignParentTop="true"
  54. android:background="#eeeeee"
  55. android:text="我跟上级顶部对齐"
  56. android:textSize="11sp"
  57. android:textColor="#000000" />
  58. <TextView
  59. android:id="@+id/tv_parent_bottom"
  60. android:layout_width="wrap_content"
  61. android:layout_height="wrap_content"
  62. android:layout_alignParentBottom="true"
  63. android:background="#eeeeee"
  64. android:text="我跟上级底部对齐"
  65. android:textSize="11sp"
  66. android:textColor="#000000" />
  67. <TextView
  68. android:id="@+id/tv_left_center"
  69. android:layout_width="wrap_content"
  70. android:layout_height="wrap_content"
  71. android:layout_toLeftOf="@+id/tv_center"
  72. android:layout_alignTop="@+id/tv_center"
  73. android:background="#eeeeee"
  74. android:text="我在中间左边"
  75. android:textSize="11sp"
  76. android:textColor="#000000" />
  77. <TextView
  78. android:id="@+id/tv_right_center"
  79. android:layout_width="wrap_content"
  80. android:layout_height="wrap_content"
  81. android:layout_toRightOf="@+id/tv_center"
  82. android:layout_alignBottom="@+id/tv_center"
  83. android:background="#eeeeee"
  84. android:text="我在中间右边"
  85. android:textSize="11sp"
  86. android:textColor="#000000" />
  87. <TextView
  88. android:id="@+id/tv_above_center"
  89. android:layout_width="wrap_content"
  90. android:layout_height="wrap_content"
  91. android:layout_above="@+id/tv_center"
  92. android:layout_alignLeft="@+id/tv_center"
  93. android:background="#eeeeee"
  94. android:text="我在中间上面"
  95. android:textColor="#000000"
  96. android:textSize="11sp" />
  97. <TextView
  98. android:id="@+id/tv_below_center"
  99. android:layout_width="wrap_content"
  100. android:layout_height="wrap_content"
  101. android:layout_below="@+id/tv_center"
  102. android:layout_alignRight="@+id/tv_center"
  103. android:background="#eeeeee"
  104. android:text="我在中间下面"
  105. android:textSize="11sp"
  106. android:textColor="#000000" />
  107. </RelativeLayout>

6cd76291d84da02ea8a2e718ccd9a743.png

=================================================================================================================

网格布局GridLayout

网格布局支持多行多列的表格排列。

网格布局默认从左往右、从上到下排列,它新增了两个属性:

(1) columnCount属性,它指定了网格的列数,即每行能放多少个视图;

(2) rowCount属性,它指定了网格的行数,即每列能放多少个视图;

布局:

  1. <!-- 根布局为两行两列的网格布局,其中列数由columnCount指定,行数由rowCount指定 -->
  2. <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:columnCount="2"
  6. android:rowCount="2">
  7. <TextView
  8. android:layout_width="100dp"
  9. android:layout_height="60dp"
  10. android:gravity="center"
  11. android:background="#ffcccc"
  12. android:text="浅红色"
  13. android:textColor="#000000"
  14. android:textSize="17sp" />
  15. <TextView
  16. android:layout_width="100dp"
  17. android:layout_height="60dp"
  18. android:gravity="center"
  19. android:background="#ffaa00"
  20. android:text="橙色"
  21. android:textColor="#000000"
  22. android:textSize="17sp" />
  23. <TextView
  24. android:layout_width="205dp"
  25. android:layout_height="60dp"
  26. android:gravity="center"
  27. android:background="#00ff00"
  28. android:text="绿色"
  29. android:textColor="#000000"
  30. android:textSize="17sp" />
  31. <TextView
  32. android:layout_width="205dp"
  33. android:layout_height="60dp"
  34. android:gravity="center"
  35. android:background="#660066"
  36. android:text="深紫色"
  37. android:textColor="#000000"
  38. android:textSize="17sp" />
  39. </GridLayout>

e903f1eac0dc114a376313d319197be3.png

布局:

  1. <!-- 根布局为两行两列的网格布局,其中列数由columnCount指定,行数由rowCount指定 -->
  2. <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:columnCount="2"
  6. android:rowCount="2">
  7. <TextView
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:gravity="center"
  11. android:background="#ffcccc"
  12. android:text="浅红色"
  13. android:layout_margin="20dp"
  14. android:textColor="#000000"
  15. android:textSize="17sp" />
  16. <TextView
  17. android:layout_width="80dp"
  18. android:layout_height="40dp"
  19. android:gravity="center"
  20. android:layout_gravity="center"
  21. android:layout_margin="40dp"
  22. android:background="#ffaa00"
  23. android:text="橙色"
  24. android:textColor="#000000"
  25. android:textSize="17sp" />
  26. <TextView
  27. android:layout_width="205dp"
  28. android:layout_height="60dp"
  29. android:gravity="center"
  30. android:background="#00ff00"
  31. android:text="绿色"
  32. android:textColor="#000000"
  33. android:textSize="17sp" />
  34. <TextView
  35. android:layout_width="205dp"
  36. android:layout_height="60dp"
  37. android:gravity="center"
  38. android:background="#660066"
  39. android:text="深紫色"
  40. android:textColor="#000000"
  41. android:textSize="17sp" />
  42. </GridLayout>

2393b89f0203a275ead5586f8b4948bd.png

布局:

  1. <!-- 根布局为两行两列的网格布局,其中列数由columnCount指定,行数由rowCount指定 -->
  2. <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:columnCount="2"
  6. android:rowCount="2">
  7. <TextView
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:gravity="center"
  11. android:background="#ffcccc"
  12. android:text="浅红色"
  13. android:layout_margin="20dp"
  14. android:textColor="#000000"
  15. android:textSize="17sp" />
  16. <TextView
  17. android:layout_width="80dp"
  18. android:layout_height="40dp"
  19. android:gravity="center"
  20. android:layout_gravity="center"
  21. android:background="#ffaa00"
  22. android:text="橙色"
  23. android:textColor="#000000"
  24. android:textSize="17sp" />
  25. <TextView
  26. android:layout_width="205dp"
  27. android:layout_height="60dp"
  28. android:gravity="center"
  29. android:background="#00ff00"
  30. android:text="绿色"
  31. android:textColor="#000000"
  32. android:textSize="17sp" />
  33. <TextView
  34. android:layout_width="205dp"
  35. android:layout_height="60dp"
  36. android:gravity="center"
  37. android:background="#660066"
  38. android:text="深紫色"
  39. android:textColor="#000000"
  40. android:textSize="17sp" />
  41. </GridLayout>

c14552df6e7610e5dc4bfdc970f2ada8.png

======================================================================================================

20a3d2732ab30c6973146135354504db.png

滚动视图有两种:

(1) ScrollView,它是垂直方向的滚动视图;垂直方向滚动时,layout_width属性值设置为match_parent,layout_height属性值设置为wrap_content。

(2) HorizontalScrollView,它是水平方向的滚动视图;水平方向滚动时,layout_width属性值设置为wrap_content,layout_height属性值设置为match_parent。

布局:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical">
  5. <!-- HorizontalScrollView是水平方向的滚动视图,当前高度为200dp -->
  6. <HorizontalScrollView
  7. android:layout_width="wrap_content"
  8. android:layout_height="200dp">
  9. <!-- 水平方向的线性布局,两个子视图的颜色分别为青色和黄色 -->
  10. <LinearLayout
  11. android:layout_width="wrap_content"
  12. android:layout_height="match_parent"
  13. android:orientation="horizontal">
  14. <View
  15. android:layout_width="300dp"
  16. android:layout_height="match_parent"
  17. android:background="#aaffff" />
  18. <View
  19. android:layout_width="300dp"
  20. android:layout_height="match_parent"
  21. android:background="#ffff00" />
  22. </LinearLayout>
  23. </HorizontalScrollView>
  24. <!-- ScrollView是垂直方向的滚动视图,当前高度为自适应 -->
  25. <ScrollView
  26. android:layout_width="match_parent"
  27. android:layout_height="wrap_content">
  28. <!-- 垂直方向的线性布局,两个子视图的颜色分别为绿色和橙色 -->
  29. <LinearLayout
  30. android:layout_width="match_parent"
  31. android:layout_height="wrap_content"
  32. android:orientation="vertical">
  33. <View
  34. android:layout_width="match_parent"
  35. android:layout_height="400dp"
  36. android:background="#00ff00" />
  37. <View
  38. android:layout_width="match_parent"
  39. android:layout_height="400dp"
  40. android:background="#ffffaa" />
  41. </LinearLayout>
  42. </ScrollView>
  43. </LinearLayout>

62e4caf9ca5505ce9b5e7cbb98b0dc9b.png

6db764b2d29d3797294b84881122d7e4.png

972b7a105cb29ccac26557a48a92480c.png

04b15b61614961be207ab88e87c2cb83.png

243ee342b055c6986b0708aa2bb9a976.png

发表评论

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

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

相关阅读