当前位置:   article > 正文

HarmonyOS学习路之开发篇—Java UI框架(StackLayout)_harmonyos5.0支持java吗

harmonyos5.0支持java吗

StackLayout

StackLayout直接在屏幕上开辟出一块空白的区域,添加到这个布局中的视图都是以层叠的方式显示,而它会把这些视图默认放到这块区域的左上角,第一个添加到布局中的视图显示在最底层,最后一个被放在最顶层。上一层的视图会覆盖下一层的视图。

支持的XML属性

StackLayout无自有的XML属性,共有XML属性继承自:Component

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

属性名称

中文描述

取值

取值说明

使用案例

layout_alignment

对齐方式

left

表示左对齐

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

ohos:layout_alignment="top"

ohos:layout_alignment="top|left"

top

表示顶部对齐。

right

表示右对齐

bottom

表示底部对齐。

horizontal_center

表示水平居中对齐。

vertical_center

表示垂直居中对齐。

center

表示居中对齐。

StackLayout的创建和使用

创建StackLayout

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <StackLayout
  3. xmlns:ohos="http://schemas.huawei.com/res/ohos"
  4. ohos:height="match_parent"
  5. ohos:width="match_parent">
  6. </StackLayout>

使用默认布局添加组件

StackLayout中组件的布局默认在区域的左上角,并且以后创建的组件会在上层。

xml布局:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <StackLayout
  3. xmlns:ohos="http://schemas.huawei.com/res/ohos"
  4. ohos:id="$+id:stack_layout"
  5. ohos:height="match_parent"
  6. ohos:width="match_parent">
  7. <Text
  8. ohos:id="$+id:text_blue"
  9. ohos:text_alignment="bottom|horizontal_center"
  10. ohos:text_size="24fp"
  11. ohos:text="Layer 1"
  12. ohos:height="400vp"
  13. ohos:width="400vp"
  14. ohos:background_element="#3F56EA" />
  15. <Text
  16. ohos:id="$+id:text_light_purple"
  17. ohos:text_alignment="bottom|horizontal_center"
  18. ohos:text_size="24fp"
  19. ohos:text="Layer 2"
  20. ohos:height="300vp"
  21. ohos:width="300vp"
  22. ohos:background_element="#00AAEE" />
  23. <Text
  24. ohos:id="$+id:text_orange"
  25. ohos:text_alignment="center"
  26. ohos:text_size="24fp"
  27. ohos:text="Layer 3"
  28. ohos:height="80vp"
  29. ohos:width="80vp"
  30. ohos:background_element="#00BFC9" />
  31. </StackLayout>

多个视图排列效果

使用相对位置添加组件

使用layout_alignment属性可以指定组件在StackLayout中的相对位置,如下表示Button组件位于StackLayout的右面。

xml布局:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <StackLayout
  3. xmlns:ohos="http://schemas.huawei.com/res/ohos"
  4. ohos:id="$+id:stack_layout"
  5. ohos:height="match_parent"
  6. ohos:width="match_parent">
  7. <Button
  8. ohos:id="$+id:button"
  9. ohos:height="40vp"
  10. ohos:width="80vp"
  11. ohos:layout_alignment="right"
  12. ohos:background_element="#3399FF"/>
  13. </StackLayout>

右边布局

场景展示

点击将子视图从底层移到顶层显示

将某个视图移到顶层的效果

 Java示例代码:

  1. ComponentContainer stackLayout = (ComponentContainer) findComponentById(ResourceTable.Id_stack_layout);
  2. Text textFirst = (Text) findComponentById(ResourceTable.Id_text_blue);
  3. textFirst.setClickedListener(new Component.ClickedListener() {
  4. @Override
  5. public void onClick(Component component) {
  6. stackLayout.moveChildToFront(component);
  7. }
  8. });

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

闽ICP备14008679号