当前位置:   article > 正文

HarmonyOS学习路之开发篇—Java UI框架(DirectionalLayout)_ohos:background_element="#ff000000" 放一个背景图片

ohos:background_element="#ff000000" 放一个背景图片

DirectionalLayout

 

DirectionalLayout是Java UI中的一种重要组件布局,用于将一组组件(Component)按照水平或者垂直方向排布,能够方便地对齐布局内的组件。该布局和其他布局的组合,可以实现更加丰富的布局方式。

                                                            DirectionalLayout示意图

支持的XML属性

DirectionalLayout的共有XML属性继承自:Component

DirectionalLayout的自有XML属性见下表:

属性名称

中文描述

取值

取值说明

使用案例

alignment对齐方式left表示左对齐。

可以设置取值项如表中所列,

也可以使用“|”进行多项组合。

ohos:alignment

="top|left"

ohos:alignment

="left"

top表示顶部对齐。
right表示右对齐
bottom表示底部对齐。
horizontal_center表示水平居中对齐。
vertical_center表示垂直居中对齐。
center表示居中对齐。
start表示靠起始端对齐。
end表示靠结束端对齐。
orientation子布局排列方向

horizontal

表示水平方向布局。

ohos:orientation

="horizontal"

vertical

表示垂直方向布局。

ohos:orientation

="vertical"

total_weight

所有子视图的权重之和

float类型

可以直接设置浮点数值,也可以引用float浮点数资源。

ohos:total_weight

="2.5"

ohos:total_weigh

t="$float:total_weight"

DirectionalLayout所包含组件可支持的XML属性见下表:

属性名称

中文描述

取值

取值说明

使用案例

layout_alignment对齐方式left表示左对齐。

可以设置取值项如表中所列,也可以使用“|”进行多项组合。

ohos:layout_alignment

="top"

ohos:layout_alignmen

t="top|left"

top表示顶部对齐。
right表示右对齐
bottom表示底部对齐。
horizontal_center表示水平居中对齐。
vertical_center表示垂直居中对齐。
center表示居中对齐。

weight

比重

float类型

可以直接设置浮点数值,也可以引用float浮点数资源。

ohos:weight

="1"

ohos:weight

="$float:weight"

排列方式

DirectionalLayout的排列方向(orientation)分为水平(horizontal)或者垂直(vertical)方向。使用orientation设置布局内组件的排列方式,默认为垂直排列。

垂直排列

垂直方向排列三个按钮,效果如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <DirectionalLayout
  3. xmlns:ohos="http://schemas.huawei.com/res/ohos"
  4. ohos:width="match_parent"
  5. ohos:height="match_content"
  6. ohos:orientation="vertical">
  7. <Button
  8. ohos:width="33vp"
  9. ohos:height="20vp"
  10. ohos:bottom_margin="3vp"
  11. ohos:left_margin="13vp"
  12. ohos:background_element="$graphic:color_cyan_element"
  13. ohos:text="Button 1"/>
  14. <Button
  15. ohos:width="33vp"
  16. ohos:height="20vp"
  17. ohos:bottom_margin="3vp"
  18. ohos:left_margin="13vp"
  19. ohos:background_element="$graphic:color_cyan_element"
  20. ohos:text="Button 2"/>
  21. <Button
  22. ohos:width="33vp"
  23. ohos:height="20vp"
  24. ohos:bottom_margin="3vp"
  25. ohos:left_margin="13vp"
  26. ohos:background_element="$graphic:color_cyan_element"
  27. ohos:text="Button 3"/>
  28. </DirectionalLayout>

color_cyan_element.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
  3. ohos:shape="rectangle">
  4. <solid
  5. ohos:color="#00FFFD"/>
  6. </shape>

水平排列

 子组件未超过布局本身大小 

水平方向排列三个按钮,效果如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <DirectionalLayout
  3. xmlns:ohos="http://schemas.huawei.com/res/ohos"
  4. ohos:width="match_parent"
  5. ohos:height="match_content"
  6. ohos:orientation="horizontal">
  7. <Button
  8. ohos:width="33vp"
  9. ohos:height="20vp"
  10. ohos:left_margin="13vp"
  11. ohos:background_element="$graphic:color_cyan_element"
  12. ohos:text="Button 1"/>
  13. <Button
  14. ohos:width="33vp"
  15. ohos:height="20vp"
  16. ohos:left_margin="13vp"
  17. ohos:background_element="$graphic:color_cyan_element"
  18. ohos:text="Button 2"/>
  19. <Button
  20. ohos:width="33vp"
  21. ohos:height="20vp"
  22. ohos:left_margin="13vp"
  23. ohos:background_element="$graphic:color_cyan_element"
  24. ohos:text="Button 3"/>
  25. </DirectionalLayout>

color_cyan_element.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
  3. ohos:shape="rectangle">
  4. <solid
  5. ohos:color="#00FFFD"/>
  6. </shape>
  • 子组件超过布局本身大小

DirectionalLayout不会自动换行,其子组件会按照设定的方向依次排列,若超过布局本身的大小,超出布局大小的部分将不会被显示。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <DirectionalLayout
  3. xmlns:ohos="http://schemas.huawei.com/res/ohos"
  4. ohos:width="match_parent"
  5. ohos:height="20vp"
  6. ohos:orientation="horizontal">
  7. <Button
  8. ohos:width="166vp"
  9. ohos:height="match_content"
  10. ohos:left_margin="13vp"
  11. ohos:background_element="$graphic:color_cyan_element"
  12. ohos:text="Button 1"/>
  13. <Button
  14. ohos:width="166vp"
  15. ohos:height="match_content"
  16. ohos:left_margin="13vp"
  17. ohos:background_element="$graphic:color_cyan_element"
  18. ohos:text="Button 2"/>
  19. <Button
  20. ohos:width="166vp"
  21. ohos:height="match_content"
  22. ohos:left_margin="13vp"
  23. ohos:background_element="$graphic:color_cyan_element"
  24. ohos:text="Button 3"/>
  25. </DirectionalLayout>

 color_cyan_element.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
  3. ohos:shape="rectangle">
  4. <solid
  5. ohos:color="#00FFFD"/>
  6. </shape>

此布局包含了三个按钮,但由于DirectionalLayout不会自动换行,超出布局大小的组件部分无法显示。界面显示如下:

对齐方式

 DirectionalLayout中的组件使用layout_alignment控制自身在布局中的对齐方式。对齐方式和排列方式密切相关,当排列方式为水平方向时,可选的对齐方式只有作用于垂直方向的类型(top、bottom、vertical_center、center)其他对齐方式不会生效。当排列方式为垂直方向时,可选的对齐方式只有作用于水平方向的类型(left、right、start、end、horizontal_center、center)其他对齐方式不会生效。

三种对齐方式的示例代码:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <DirectionalLayout
  3. xmlns:ohos="http://schemas.huawei.com/res/ohos"
  4. ohos:width="match_parent"
  5. ohos:height="60vp">
  6. <Button
  7. ohos:width="50vp"
  8. ohos:height="20vp"
  9. ohos:background_element="$graphic:color_cyan_element"
  10. ohos:layout_alignment="left"
  11. ohos:text="Button 1"/>
  12. <Button
  13. ohos:width="50vp"
  14. ohos:height="20vp"
  15. ohos:background_element="$graphic:color_cyan_element"
  16. ohos:layout_alignment="horizontal_center"
  17. ohos:text="Button 2"/>
  18. <Button
  19. ohos:width="50vp"
  20. ohos:height="20vp"
  21. ohos:background_element="$graphic:color_cyan_element"
  22. ohos:layout_alignment="right"
  23. ohos:text="Button 3"/>
  24. </DirectionalLayout>

color_cyan_element.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
  3. ohos:shape="rectangle">
  4. <solid
  5. ohos:color="#00FFFD"/>
  6. </shape>

三种对齐方式效果示例

 权重

权重(weight)就是按比例来分配组件占用父组件的大小,在水平布局下计算公式为:

父布局可分配宽度=父布局宽度-所有子组件width之和;

组件宽度=组件weight/所有组件weight之和*父布局可分配宽度;

实际使用过程中,建议使用width=0来按比例分配父布局的宽度,1:1:1效果如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <DirectionalLayout
  3. xmlns:ohos="http://schemas.huawei.com/res/ohos"
  4. ohos:width="match_parent"
  5. ohos:height="match_content"
  6. ohos:orientation="horizontal">
  7. <Button
  8. ohos:width="0vp"
  9. ohos:height="20vp"
  10. ohos:weight="1"
  11. ohos:background_element="$graphic:color_cyan_element"
  12. ohos:text="Button 1"/>
  13. <Button
  14. ohos:width="0vp"
  15. ohos:height="20vp"
  16. ohos:weight="1"
  17. ohos:background_element="$graphic:color_gray_element"
  18. ohos:text="Button 2"/>
  19. <Button
  20. ohos:width="0vp"
  21. ohos:height="20vp"
  22. ohos:weight="1"
  23. ohos:background_element="$graphic:color_cyan_element"
  24. ohos:text="Button 3"/>
  25. </DirectionalLayout>

 color_cyan_element.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
  3. ohos:shape="rectangle">
  4. <solid
  5. ohos:color="#00FFFD"/>
  6. </shape>

color_gray_element.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
  3. ohos:shape="rectangle">
  4. <solid
  5. ohos:color="#878787"/>
  6. </shape>

场景示例

 源码示例:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <DirectionalLayout
  3. xmlns:ohos="http://schemas.huawei.com/res/ohos"
  4. ohos:height="match_parent"
  5. ohos:width="match_parent"
  6. ohos:background_element="#FFFFFFFF">
  7. <DirectionalLayout
  8. ohos:height="70vp"
  9. ohos:width="match_parent"
  10. ohos:orientation="vertical"
  11. ohos:background_element="#FF9F9F9F"
  12. ohos:top_margin="10vp">
  13. <Button
  14. ohos:height="20vp"
  15. ohos:width="33vp"
  16. ohos:background_element="#FF00FFFD"
  17. ohos:bottom_margin="3vp"
  18. ohos:left_margin="13vp"
  19. ohos:text="Button 1"/>
  20. <Button
  21. ohos:height="20vp"
  22. ohos:width="33vp"
  23. ohos:background_element="#FF00FFFD"
  24. ohos:bottom_margin="3vp"
  25. ohos:left_margin="13vp"
  26. ohos:text="Button 2"/>
  27. <Button
  28. ohos:height="20vp"
  29. ohos:width="33vp"
  30. ohos:background_element="#FF00FFFD"
  31. ohos:bottom_margin="3vp"
  32. ohos:left_margin="13vp"
  33. ohos:text="Button 3"/>
  34. </DirectionalLayout>
  35. <DirectionalLayout
  36. ohos:height="70vp"
  37. ohos:width="match_parent"
  38. ohos:orientation="horizontal"
  39. ohos:background_element="#FF9F9F9F"
  40. ohos:top_margin="10vp"
  41. >
  42. <Button
  43. ohos:height="20vp"
  44. ohos:width="33vp"
  45. ohos:background_element="#FF00FFFD"
  46. ohos:left_margin="13vp"
  47. ohos:text="Button 1"/>
  48. <Button
  49. ohos:height="20vp"
  50. ohos:width="33vp"
  51. ohos:background_element="#FF00FFFD"
  52. ohos:left_margin="13vp"
  53. ohos:text="Button 2"/>
  54. <Button
  55. ohos:height="20vp"
  56. ohos:width="33vp"
  57. ohos:background_element="#FF00FFFD"
  58. ohos:left_margin="13vp"
  59. ohos:text="Button 3"/>
  60. </DirectionalLayout>
  61. <DirectionalLayout
  62. ohos:height="70vp"
  63. ohos:width="match_parent"
  64. ohos:orientation="horizontal"
  65. ohos:background_element="#FF9F9F9F"
  66. ohos:top_margin="10vp"
  67. >
  68. <Button
  69. ohos:height="match_content"
  70. ohos:width="166vp"
  71. ohos:background_element="#FF00FFFD"
  72. ohos:left_margin="13vp"
  73. ohos:text="Button 1"/>
  74. <Button
  75. ohos:height="match_content"
  76. ohos:width="166vp"
  77. ohos:background_element="#FF00FFFD"
  78. ohos:left_margin="13vp"
  79. ohos:text="Button 2"/>
  80. <Button
  81. ohos:height="match_content"
  82. ohos:width="166vp"
  83. ohos:background_element="#FF00FFFD"
  84. ohos:left_margin="13vp"
  85. ohos:text="Button 3"/>
  86. </DirectionalLayout>
  87. <DirectionalLayout
  88. ohos:height="70vp"
  89. ohos:width="match_parent"
  90. ohos:background_element="#FF9F9F9F"
  91. ohos:top_margin="10vp"
  92. >
  93. <Button
  94. ohos:height="20vp"
  95. ohos:width="50vp"
  96. ohos:background_element="#FF00FFFD"
  97. ohos:layout_alignment="left"
  98. ohos:text="Button 1"/>
  99. <Button
  100. ohos:height="20vp"
  101. ohos:width="50vp"
  102. ohos:background_element="#FF00FFFD"
  103. ohos:layout_alignment="horizontal_center"
  104. ohos:text="Button 2"/>
  105. <Button
  106. ohos:height="20vp"
  107. ohos:width="50vp"
  108. ohos:background_element="#FF00FFFD"
  109. ohos:layout_alignment="right"
  110. ohos:text="Button 3"/>
  111. </DirectionalLayout>
  112. <DirectionalLayout
  113. ohos:height="70vp"
  114. ohos:width="match_parent"
  115. ohos:orientation="horizontal"
  116. ohos:background_element="#FF9F9F9F"
  117. ohos:top_margin="10vp"
  118. >
  119. <Button
  120. ohos:height="20vp"
  121. ohos:width="0vp"
  122. ohos:background_element="#FF00FFFD"
  123. ohos:text="Button 1"
  124. ohos:weight="1"/>
  125. <Button
  126. ohos:height="20vp"
  127. ohos:width="0vp"
  128. ohos:background_element="#FFFF8A8A"
  129. ohos:text="Button 2"
  130. ohos:weight="1"/>
  131. <Button
  132. ohos:height="20vp"
  133. ohos:width="0vp"
  134. ohos:background_element="#FF00FFFD"
  135. ohos:text="Button 3"
  136. ohos:weight="1"/>
  137. </DirectionalLayout>
  138. </DirectionalLayout>

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