IDEA Android 线性布局(LinearLayout)示例

男娘i 2023-10-04 22:32 153阅读 0赞

ConstraintLayout是目前Android Studio(3.2+)以及IDEA中默认的布局方式,如果你新建项目Android Studio会自动使用ConstraintLayout作为你的默认根布局,但是如果你一定要使用线性布局呢?

先上效果图(总共9个按钮交叉混合分布):

a07a057fa06d488b95465c09a7c2c66f.png

activity_main.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical"
  6. android:background="#ffffff"
  7. android:layout_margin="5dp"
  8. >
  9. <LinearLayout
  10. android:id="@+id/LinearLayout01"
  11. android:layout_width="fill_parent"
  12. android:layout_height="fill_parent"
  13. android:orientation="horizontal"
  14. android:layout_weight="1.0"
  15. >
  16. <Button
  17. android:layout_width="fill_parent"
  18. android:layout_height="fill_parent"
  19. android:gravity="center_horizontal"
  20. android:text="按钮1"
  21. android:layout_weight="1.0"
  22. android:layout_margin="5dp"
  23. />
  24. <Button
  25. android:layout_width="fill_parent"
  26. android:layout_height="fill_parent"
  27. android:gravity="center_horizontal"
  28. android:text="按钮2"
  29. android:layout_weight="1.0"
  30. android:layout_margin="5dp"
  31. />
  32. <Button
  33. android:layout_width="fill_parent"
  34. android:layout_height="fill_parent"
  35. android:gravity="center_horizontal"
  36. android:text="按钮3"
  37. android:layout_weight="1.0"
  38. android:layout_margin="5dp"
  39. />
  40. </LinearLayout>
  41. <LinearLayout
  42. android:id="@+id/LinearLayout02"
  43. android:layout_width="fill_parent"
  44. android:layout_height="wrap_content"
  45. android:orientation="vertical">
  46. <Button
  47. android:layout_width="fill_parent"
  48. android:layout_height="wrap_content"
  49. android:text="按钮4"
  50. android:layout_weight="1.0"
  51. />
  52. <Button
  53. android:layout_width="fill_parent"
  54. android:layout_height="wrap_content"
  55. android:text="按钮5"
  56. android:layout_weight="1.0"
  57. />
  58. <Button
  59. android:layout_width="fill_parent"
  60. android:layout_height="wrap_content"
  61. android:text="按钮6"
  62. android:layout_weight="1.0"
  63. />
  64. </LinearLayout>
  65. <LinearLayout
  66. android:id="@+id/LinearLayout03"
  67. android:layout_width="fill_parent"
  68. android:layout_height="wrap_content"
  69. android:orientation="horizontal">
  70. <Button
  71. android:layout_width="fill_parent"
  72. android:layout_height="wrap_content"
  73. android:text="按钮7"
  74. android:layout_weight="1.0"
  75. />
  76. <Button
  77. android:layout_width="fill_parent"
  78. android:layout_height="wrap_content"
  79. android:text="按钮8"
  80. android:layout_weight="1.0"
  81. android:layout_marginLeft="5dp"
  82. android:layout_marginRight="5dp"
  83. />
  84. <Button
  85. android:layout_width="fill_parent"
  86. android:layout_height="wrap_content"
  87. android:text="按钮9"
  88. android:layout_weight="1.0"
  89. />
  90. </LinearLayout>
  91. </LinearLayout>

发表评论

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

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

相关阅读