当前位置:   article > 正文

20.HarmonyOS App(JAVA)表格布局Layout使用方法

20.HarmonyOS App(JAVA)表格布局Layout使用方法

ability_main.xml,实现计算器键盘按钮

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TableLayout
  3. xmlns:ohos="http://schemas.huawei.com/res/ohos"
  4. ohos:height="match_parent"
  5. ohos:width="match_parent"
  6. ohos:row_count="6"
  7. ohos:column_count="4"
  8. >
  9. <Button
  10. ohos:id="$+id:btn_table"
  11. ohos:height="35vp"
  12. ohos:width="65vp"
  13. ohos:text = "表格布局_计算器键盘"
  14. ohos:background_element="#FF374FF1"
  15. ohos:text_size="20fp"
  16. ohos:text_color="#FFFDFCFC"
  17. ohos:margin="6vp"
  18. />
  19. <Button
  20. ohos:height="35vp"
  21. ohos:width="65vp"
  22. ohos:text = "7"
  23. ohos:background_element="#FF7BA4CF"
  24. ohos:text_size="20fp"
  25. ohos:margin="6vp"
  26. />
  27. <Button
  28. ohos:height="35vp"
  29. ohos:width="65vp"
  30. ohos:text = "8"
  31. ohos:background_element="#FF7BA4CF"
  32. ohos:text_size="20fp"
  33. ohos:margin="6vp"
  34. />
  35. <Button
  36. ohos:height="35vp"
  37. ohos:width="65vp"
  38. ohos:text = "9"
  39. ohos:background_element="#FF7BA4CF"
  40. ohos:text_size="20fp"
  41. ohos:margin="6vp"
  42. />
  43. <Button
  44. ohos:height="35vp"
  45. ohos:width="65vp"
  46. ohos:text = "/"
  47. ohos:background_element="#FF7BA4CF"
  48. ohos:text_size="20fp"
  49. ohos:margin="6vp"
  50. />
  51. <Button
  52. ohos:height="35vp"
  53. ohos:width="65vp"
  54. ohos:text = "4"
  55. ohos:background_element="#FF59EC23"
  56. ohos:text_size="20fp"
  57. ohos:margin="6vp"
  58. />
  59. <Button
  60. ohos:height="35vp"
  61. ohos:width="65vp"
  62. ohos:text = "5"
  63. ohos:background_element="#FF59EC23"
  64. ohos:text_size="20fp"
  65. ohos:margin="6vp"
  66. />
  67. <Button
  68. ohos:height="35vp"
  69. ohos:width="65vp"
  70. ohos:text = "6"
  71. ohos:background_element="#FF59EC23"
  72. ohos:text_size="20fp"
  73. ohos:margin="6vp"
  74. />
  75. <Button
  76. ohos:height="35vp"
  77. ohos:width="65vp"
  78. ohos:text = "*"
  79. ohos:background_element="#FF59EC23"
  80. ohos:text_size="20fp"
  81. ohos:margin="6vp"
  82. />
  83. <Button
  84. ohos:height="35vp"
  85. ohos:width="65vp"
  86. ohos:text = "1"
  87. ohos:background_element="#FF7BA4CF"
  88. ohos:text_size="20fp"
  89. ohos:margin="6vp"
  90. />
  91. <Button
  92. ohos:height="35vp"
  93. ohos:width="65vp"
  94. ohos:text = "2"
  95. ohos:background_element="#FF7BA4CF"
  96. ohos:text_size="20fp"
  97. ohos:margin="6vp"
  98. />
  99. <Button
  100. ohos:height="35vp"
  101. ohos:width="65vp"
  102. ohos:text = "3"
  103. ohos:background_element="#FF7BA4CF"
  104. ohos:text_size="20fp"
  105. ohos:margin="6vp"
  106. />
  107. <Button
  108. ohos:height="35vp"
  109. ohos:width="65vp"
  110. ohos:text = "-"
  111. ohos:background_element="#FF7BA4CF"
  112. ohos:text_size="20fp"
  113. ohos:margin="6vp"
  114. />
  115. <Button
  116. ohos:height="35vp"
  117. ohos:width="65vp"
  118. ohos:text = "0"
  119. ohos:background_element="#FF59EC23"
  120. ohos:text_size="20fp"
  121. ohos:margin="6vp"
  122. />
  123. <Button
  124. ohos:height="35vp"
  125. ohos:width="65vp"
  126. ohos:text = "."
  127. ohos:background_element="#FF59EC23"
  128. ohos:text_size="20fp"
  129. ohos:margin="6vp"
  130. />
  131. <Button
  132. ohos:height="35vp"
  133. ohos:width="65vp"
  134. ohos:text = "+"
  135. ohos:background_element="#FF59EC23"
  136. ohos:text_size="20fp"
  137. ohos:margin="6vp"
  138. />
  139. <Button
  140. ohos:height="35vp"
  141. ohos:width="65vp"
  142. ohos:text = "="
  143. ohos:background_element="#FF59EC23"
  144. ohos:text_size="20fp"
  145. ohos:margin="6vp"
  146. />
  147. <Button
  148. ohos:id="$+id:btn_clear"
  149. ohos:height="35vp"
  150. ohos:width="65vp"
  151. ohos:text = "clear"
  152. ohos:background_element="#FF7BA4CF"
  153. ohos:text_size="20fp"
  154. ohos:margin="6vp"
  155. />
  156. </TableLayout>

 MainAbilitySlice.java

点击按钮,toast消息提示,设置按钮控件跨列效果

  1. package com.example.myapplication.slice;
  2. import com.example.myapplication.ResourceTable;
  3. import ohos.aafwk.ability.AbilitySlice;
  4. import ohos.aafwk.content.Intent;
  5. import ohos.agp.components.Button;
  6. import ohos.agp.components.Component;
  7. import ohos.agp.components.TableLayout;
  8. import ohos.agp.utils.LayoutAlignment;
  9. import ohos.agp.window.dialog.ToastDialog;
  10. import static ohos.agp.components.TableLayout.*;
  11. public class MainAbilitySlice extends AbilitySlice {
  12. @Override
  13. public void onStart(Intent intent) {
  14. super.onStart(intent);
  15. super.setUIContent(ResourceTable.Layout_ability_main);
  16. Button button_table = (Button) findComponentById(ResourceTable.Id_btn_table);
  17. Button button_clear = (Button) findComponentById(ResourceTable.Id_btn_clear);
  18. TableLayout.LayoutConfig config = new TableLayout.LayoutConfig(TableLayout.specification(0,1),TableLayout.specification(0,4));
  19. //TableLayout.specification(4,1),行规范
  20. //TableLayout.specification(0,4),列规范
  21. //设置宽度
  22. config.width = button_table.getWidth()*4 + button_table.getMarginLeft()*6;
  23. //设置高度
  24. config.height = button_table.getHeight();
  25. //设置外边框
  26. config.setMargins(button_table.getMarginLeft(),button_table.getMarginTop(),button_table.getMarginRight(),button_table.getMarginBottom());
  27. button_table.setLayoutConfig(config);
  28. TableLayout.LayoutConfig config2 = new TableLayout.LayoutConfig(TableLayout.specification(5,1),TableLayout.specification(0,4));
  29. //TableLayout.specification(4,1),行规范
  30. //TableLayout.specification(0,4),列规范
  31. //设置宽度
  32. config2.width = button_clear.getWidth()*4 + button_clear.getMarginLeft()*6;
  33. //设置高度
  34. config2.height = button_clear.getHeight();
  35. //设置外边框
  36. config2.setMargins(button_clear.getMarginLeft(),button_clear.getMarginTop(),button_clear.getMarginRight(),button_clear.getMarginBottom());
  37. button_clear.setLayoutConfig(config2);
  38. button_clear.setClickedListener(new ClickedListener() {
  39. @Override
  40. public void onClick(Component component) {
  41. new ToastDialog(getContext())
  42. .setText("点击了清除按钮")
  43. .setAlignment(LayoutAlignment.CENTER)
  44. .show();
  45. }
  46. });
  47. }
  48. @Override
  49. public void onActive() {
  50. super.onActive();
  51. }
  52. @Override
  53. public void onForeground(Intent intent) {
  54. super.onForeground(intent);
  55. }
  56. }

 TableLayout的自有XML属性见下表:

表1 TableLayout的自有XML属性

属性名称

中文描述

取值

取值说明

使用案例

alignment_type

对齐方式

align_edges

表示TableLayout内的组件按边界对齐。

ohos:alignment_type="align_edges"

align_contents

表示TableLayout内的组件按边距对齐。

ohos:alignment_type="align_contents"

column_count

列数

integer类型

可以直接设置整型数值,也可以引用integer资源。

ohos:column_count="3"

ohos:column_count="$integer:count"

row_count

行数

integer类型

可以直接设置整型数值,也可以引用integer资源。

ohos:row_count="2"

ohos:row_count="$integer:count"

orientation

排列方向

horizontal

表示水平方向布局。

ohos:orientation="horizontal"

vertical

表示垂直方向布局。

ohos:orientation="vertical"

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/机器思考机器/article/detail/63330
推荐阅读
相关标签
  

闽ICP备14008679号