LinearLayout(线性布局)

ゝ一世哀愁。 2022-06-04 00:44 424阅读 0赞

LinearLayout(线性布局)

要点:

android:orientation=”vertical“垂直线性布局,”horizontal“水平线性布局

android:gravity=”top”(buttom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical、clip_horizontal)控制布局中控件的对齐方式。如果是没有子控件的控件设置此属性,表示其内容的对齐方式,比如说TextView里面文字的对齐方式;若是有子控件的控件设置此属性,则表示其子控件的对齐方式,gravity如果需要设置多个属性值,需要使用“|”进行组合

android:gravity 与 android:layout_gravity的区别
android:gravity是指定本元素的子元素相对它的对齐方式。
android:layout_gravity是指定本元素相对它的父元素的对齐方式。

android:layout_weight=”1”通过设置控件的layout_weight属性以控制各个控件在布局中的相对大小,线性布局会根据该控件layout_weight值与其所处布局中所有控件layout_weight值之和的比值为该控件分配占用的区域。在水平布局的LinearLayout中有两个Button,这两个Button的layout_weight属性值都为1,那么这两个按钮都会被拉伸到整个屏幕宽度的一半。如果layout_weight指为0,控件会按原大小显示,不会被拉伸;对于其余layout_weight属性值大于0的控件,系统将会减去layout_weight属性值为0的控件的宽度或者高度,再用剩余的宽度或高度按相应的比例来分配每一个控件显示的宽度或高度。

例:

17145723-79b4f8eca2da41e783403350809244ed.x-png

布局代码:

复制代码

  1. 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. 2 xmlns:tools="http://schemas.android.com/tools"
  3. 3 android:layout_width="match_parent"
  4. 4 android:layout_height="match_parent"
  5. 5 android:orientation="vertical"
  6. 6 tools:context=".LinearLayoutActivity" >
  7. 7
  8. 8 <LinearLayout
  9. 9 android:layout_width="match_parent"
  10. 10 android:layout_height="match_parent"
  11. 11 android:layout_weight="1"
  12. 12 android:orientation="horizontal" >
  13. 13
  14. 14 <Button
  15. 15 android:layout_width="wrap_content"
  16. 16 android:layout_height="match_parent"
  17. 17 android:layout_weight="1"
  18. 18 android:background="#aa0000"
  19. 19 android:gravity="center_horizontal|center_vertical"
  20. 20 android:text="第一列"
  21. 21 android:textSize="15sp" >
  22. 22 </Button>
  23. 23
  24. 24 <Button
  25. 25 android:layout_width="wrap_content"
  26. 26 android:layout_height="match_parent"
  27. 27 android:layout_weight="1"
  28. 28 android:background="#00aa00"
  29. 29 android:gravity="center_horizontal"
  30. 30 android:text="第二列"
  31. 31 android:textSize="15sp" >
  32. 32 </Button>
  33. 33
  34. 34 <Button
  35. 35 android:layout_width="wrap_content"
  36. 36 android:layout_height="match_parent"
  37. 37 android:layout_weight="1"
  38. 38 android:background="#0000aa"
  39. 39 android:gravity="center|bottom"
  40. 40 android:text="第三列"
  41. 41 android:textSize="15sp" >
  42. 42 </Button>
  43. 43
  44. 44 <Button
  45. 45 android:layout_width="wrap_content"
  46. 46 android:layout_height="match_parent"
  47. 47 android:layout_weight="1"
  48. 48 android:background="#aaaa00"
  49. 49 android:gravity="bottom"
  50. 50 android:text="第四列"
  51. 51 android:textSize="15sp" >
  52. 52 </Button>
  53. 53 </LinearLayout>
  54. 54
  55. 55 <LinearLayout
  56. 56 android:layout_width="match_parent"
  57. 57 android:layout_height="match_parent"
  58. 58 android:layout_weight="1"
  59. 59 android:orientation="vertical" >
  60. 60
  61. 61 <Button
  62. 62 android:layout_width="match_parent"
  63. 63 android:layout_height="match_parent"
  64. 64 android:layout_weight="1"
  65. 65 android:gravity="bottom"
  66. 66 android:text="第1行"
  67. 67 android:textSize="15sp" >
  68. 68 </Button>
  69. 69
  70. 70 <Button
  71. 71 android:layout_width="match_parent"
  72. 72 android:layout_height="match_parent"
  73. 73 android:layout_weight="1"
  74. 74 android:gravity="bottom"
  75. 75 android:text="第2行"
  76. 76 android:textSize="15sp" >
  77. 77 </Button>
  78. 78
  79. 79 <Button
  80. 80 android:layout_width="match_parent"
  81. 81 android:layout_height="match_parent"
  82. 82 android:layout_weight="1"
  83. 83 android:gravity="bottom"
  84. 84 android:text="第3行"
  85. 85 android:textSize="15sp" >
  86. 86 </Button>
  87. 87
  88. 88 <Button
  89. 89 android:layout_width="match_parent"
  90. 90 android:layout_height="match_parent"
  91. 91 android:layout_weight="1"
  92. 92 android:gravity="bottom"
  93. 93 android:text="第4行"
  94. 94 android:textSize="15sp" >
  95. 95 </Button>
  96. 96 </LinearLayout>
  97. 97
  98. 98 </LinearLayout>

发表评论

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

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

相关阅读