《网格布局》

古城微笑少年丶 2024-04-01 18:16 158阅读 0赞

文章目录

  • 一、学习要点
  • 二、课程记录
    • 1、常用属性
    • 2、创建安卓应用
    • 3、字符串资源文件
    • 4、定义边框配置文件
    • 5、主布局资源文件
    • 6、最终效果

一、学习要点

  1. 网格布局特点
  2. 网格布局常用属性
  3. 案例演示

二、课程记录

1、常用属性










































属性 含义
rowCount 行数
columnCount 列数
layout_rowSpan 跨行数
layout_columnSpan 跨列数
layout_width 布局宽度
layout_height 布局高度
layout_row 子控件在布局的行数
layout_column 子控件在布局的列数

2、创建安卓应用

  • 基于Empty Activity模板创建- GridLayoutCalculator
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

3、字符串资源文件

  • strings.xml
    在这里插入图片描述


    网络布局:计算器界面

4、定义边框配置文件

  • drawable目录中添加custom_border.xml
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    <?xml version=”1.0” encoding=”utf-8”?>









5、主布局资源文件

  • 约束布局修改为垂直的线性布局并设置对应属性
    在这里插入图片描述
    在这里插入图片描述
  • 添加一个用于显示运算结果的标签
    在这里插入图片描述
    在这里插入图片描述
  • 添加一个网格布局,设置为6行5列
    在这里插入图片描述
  • 为第一行 添加五个按钮
    在这里插入图片描述
  • 预览效果
    在这里插入图片描述
  • 为第二行添加五个按钮
    在这里插入图片描述
  • 预览效果
    在这里插入图片描述
  • 为第三行添加五个按钮
    在这里插入图片描述
  • 预览效果
    在这里插入图片描述
  • 为第四行添加五个按钮
    在这里插入图片描述
  • 预览效果
    在这里插入图片描述
  • 为第五行添加五个按钮(第五个按钮跨两行)
    在这里插入图片描述
  • 查看预览效果
    在这里插入图片描述
  • 为第六行添加三个按钮(第一个按钮跨两列)
    在这里插入图片描述
  • 查看预览
    在这里插入图片描述

    <?xml version=”1.0” encoding=”utf-8”?>

    1. <TextView
    2. android:layout_width="match_parent"
    3. android:layout_height="wrap_content"
    4. android:background="@drawable/custom_border"
    5. android:layout_marginTop="30dp"
    6. android:gravity="right"
    7. android:text="01234567"
    8. android:textColor="#0000ff"
    9. android:textSize="15sp"/>
    10. <GridLayout
    11. android:layout_width="match_parent"
    12. android:layout_height="wrap_content"
    13. android:layout_marginTop="10dp"
    14. android:columnCount="5"
    15. android:rowCount="6" >
    16. <Button
    17. android:layout_width="68dp"
    18. android:layout_height="wrap_content"
    19. android:layout_marginRight="5dp"
    20. android:layout_row="0"
    21. android:layout_column="0"
    22. android:text="MC"
    23. android:textSize="15sp"/>
    24. <Button
    25. android:layout_width="68dp"
    26. android:layout_height="wrap_content"
    27. android:layout_marginRight="5dp"
    28. android:layout_row="0"
    29. android:layout_column="1"
    30. android:text="MR"
    31. android:textSize="15sp"/>
    32. <Button
    33. android:layout_width="68dp"
    34. android:layout_height="wrap_content"
    35. android:layout_marginRight="5dp"
    36. android:layout_row="0"
    37. android:layout_column="2"
    38. android:text="MS"
    39. android:textSize="15sp"/>
    40. <Button
    41. android:layout_width="68dp"
    42. android:layout_height="wrap_content"
    43. android:layout_marginRight="5dp"
    44. android:layout_row="0"
    45. android:layout_column="3"
    46. android:text="M+"
    47. android:textSize="15sp"/>
    48. <Button
    49. android:layout_width="68dp"
    50. android:layout_height="wrap_content"
    51. android:layout_row="0"
    52. android:layout_column="4"
    53. android:text="M-"
    54. android:textSize="15sp"/>
  1. <!-- -->
  2. <Button
  3. android:layout_width="68dp"
  4. android:layout_height="wrap_content"
  5. android:layout_marginRight="4dp"
  6. android:layout_row="1"
  7. android:layout_column="0"
  8. android:text="←"
  9. android:textSize="15sp"/>
  10. <Button
  11. android:layout_width="68dp"
  12. android:layout_height="wrap_content"
  13. android:layout_marginRight="4dp"
  14. android:layout_row="1"
  15. android:layout_column="1"
  16. android:text="CE"
  17. android:textSize="15sp"/>
  18. <Button
  19. android:layout_width="68dp"
  20. android:layout_height="wrap_content"
  21. android:layout_marginRight="4dp"
  22. android:layout_row="1"
  23. android:layout_column="2"
  24. android:text="C"
  25. android:textSize="15sp"/>
  26. <Button
  27. android:layout_width="68dp"
  28. android:layout_height="wrap_content"
  29. android:layout_marginRight="4dp"
  30. android:layout_row="1"
  31. android:layout_column="3"
  32. android:text="±"
  33. android:textSize="15sp"/>
  34. <Button
  35. android:layout_width="68dp"
  36. android:layout_height="wrap_content"
  37. android:layout_row="1"
  38. android:layout_column="4"
  39. android:text="√"
  40. android:textSize="15sp"/>
  41. <Button
  42. android:layout_width="68dp"
  43. android:layout_height="wrap_content"
  44. android:layout_marginRight="4dp"
  45. android:layout_row="2"
  46. android:layout_column="0"
  47. android:text="7"
  48. android:textSize="15sp"/>
  49. <Button
  50. android:layout_width="68dp"
  51. android:layout_height="wrap_content"
  52. android:layout_marginRight="4dp"
  53. android:layout_row="2"
  54. android:layout_column="1"
  55. android:text="8"
  56. android:textSize="15sp"/>
  57. <Button
  58. android:layout_width="68dp"
  59. android:layout_height="wrap_content"
  60. android:layout_marginRight="4dp"
  61. android:layout_row="2"
  62. android:layout_column="2"
  63. android:text="9"
  64. android:textSize="15sp"/>
  65. <Button
  66. android:layout_width="68dp"
  67. android:layout_height="wrap_content"
  68. android:layout_marginRight="4dp"
  69. android:layout_row="2"
  70. android:layout_column="3"
  71. android:text="/"
  72. android:textSize="15sp"/>
  73. <Button
  74. android:layout_width="68dp"
  75. android:layout_height="wrap_content"
  76. android:layout_row="2"
  77. android:layout_column="4"
  78. android:text="%"
  79. android:textSize="15sp"/>
  80. <!-- 4-->
  81. <Button
  82. android:layout_width="68dp"
  83. android:layout_height="wrap_content"
  84. android:layout_marginRight="4dp"
  85. android:layout_row="3"
  86. android:layout_column="0"
  87. android:text="4"
  88. android:textSize="20sp"/>
  89. <Button
  90. android:layout_width="68dp"
  91. android:layout_height="wrap_content"
  92. android:layout_marginRight="4dp"
  93. android:layout_row="3"
  94. android:layout_column="1"
  95. android:text="5"
  96. android:textSize="15sp"/>
  97. <Button
  98. android:layout_width="68dp"
  99. android:layout_height="wrap_content"
  100. android:layout_marginRight="4dp"
  101. android:layout_row="3"
  102. android:layout_column="2"
  103. android:text="6"
  104. android:textSize="15sp"/>
  105. <Button
  106. android:layout_width="68dp"
  107. android:layout_height="wrap_content"
  108. android:layout_marginRight="4dp"
  109. android:layout_row="3"
  110. android:layout_column="3"
  111. android:text="*"
  112. android:textSize="15sp"/>
  113. <Button
  114. android:layout_width="68dp"
  115. android:layout_height="wrap_content"
  116. android:layout_row="3"
  117. android:layout_column="4"
  118. android:text="1/x"
  119. android:textSize="15sp"/>
  120. <!-- 5 -->
  121. <Button
  122. android:layout_width="68dp"
  123. android:layout_height="wrap_content"
  124. android:layout_marginRight="4dp"
  125. android:layout_row="4"
  126. android:layout_column="0"
  127. android:text="1"
  128. android:textSize="15sp"/>
  129. <Button
  130. android:layout_width="68dp"
  131. android:layout_height="wrap_content"
  132. android:layout_marginRight="4dp"
  133. android:layout_row="4"
  134. android:layout_column="1"
  135. android:text="2"
  136. android:textSize="15sp"/>
  137. <Button
  138. android:layout_width="68dp"
  139. android:layout_height="wrap_content"
  140. android:layout_marginRight="4dp"
  141. android:layout_row="4"
  142. android:layout_column="2"
  143. android:text="3"
  144. android:textSize="15sp"/>
  145. <Button
  146. android:layout_width="68dp"
  147. android:layout_height="wrap_content"
  148. android:layout_marginRight="4dp"
  149. android:layout_row="4"
  150. android:layout_column="3"
  151. android:text="-"
  152. android:textSize="15sp"/>
  153. <Button
  154. android:layout_width="68dp"
  155. android:layout_height="97dp"
  156. android:layout_row="4"
  157. android:layout_rowSpan="2"
  158. android:layout_column="4"
  159. android:text="="
  160. android:textSize="15sp"/>
  161. <!-- 6 -->
  162. <Button
  163. android:layout_width="140dp"
  164. android:layout_height="wrap_content"
  165. android:layout_marginRight="4dp"
  166. android:layout_row="5"
  167. android:layout_columnSpan="2"
  168. android:layout_column="0"
  169. android:text="0"
  170. android:textSize="15sp"/>
  171. <Button
  172. android:layout_width="68dp"
  173. android:layout_height="wrap_content"
  174. android:layout_marginRight="4dp"
  175. android:layout_row="5"
  176. android:layout_column="2"
  177. android:text="."
  178. android:textSize="15sp"/>
  179. <Button
  180. android:layout_width="68dp"
  181. android:layout_height="wrap_content"
  182. android:layout_marginRight="4dp"
  183. android:layout_row="5"
  184. android:layout_column="3"
  185. android:text="+"
  186. android:textSize="15sp"/>
  187. </GridLayout>
  188. </LinearLayout>

6、最终效果

在这里插入图片描述

发表评论

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

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

相关阅读

    相关 Grid网格布局

    简介 > Flex布局时一维的布局,即水平或垂直方向的布局。而Grid布局则划分成“行”和“列”,产生单元格,可以看作是二维的布局。Grid布局远比Flex布局强大

    相关 bootstrap之网格布局

    一. 实现原理   网格布局是通过容器的大小,平均分为12份(可以修改),再调整内外边距,和表格布局有点类似但是也存在区别。   实现步骤如下:   (1) 数据行.ro

    相关 css grid网格布局

    初次认识grid布局,看了官方的文档也很明了([官方文档][Link 1]),然后通过自己的语言和理解,重新对他进行了总结。 > 摘要:网格布局是网站设计的基础,CSS网格模