HarmonyOS学习路之开发篇—Java UI框架(StackLayout)

StackLayout是用于创建层叠效果的布局,其中组件按照添加顺序从前向后堆叠。默认情况下,组件在左上角开始,并且后面的组件会覆盖前面的。文章介绍了StackLayout的XML属性,如layout_alignment用于调整组件对齐方式,以及如何通过设置该属性来改变组件在布局中的相对位置。还提供了示例代码展示了如何将视图移动到顶层显示。
摘要由CSDN通过智能技术生成

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

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

使用默认布局添加组件

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

xml布局:

<?xml version="1.0" encoding="utf-8"?>
<StackLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:id="$+id:stack_layout"
    ohos:height="match_parent"
    ohos:width="match_parent">

    <Text
        ohos:id="$+id:text_blue"
        ohos:text_alignment="bottom|horizontal_center"
        ohos:text_size="24fp"
        ohos:text="Layer 1"
        ohos:height="400vp"
        ohos:width="400vp"
        ohos:background_element="#3F56EA" />

    <Text
        ohos:id="$+id:text_light_purple"
        ohos:text_alignment="bottom|horizontal_center"
        ohos:text_size="24fp"
        ohos:text="Layer 2"
        ohos:height="300vp"
        ohos:width="300vp"
        ohos:background_element="#00AAEE" />

    <Text
        ohos:id="$+id:text_orange"
        ohos:text_alignment="center"
        ohos:text_size="24fp"
        ohos:text="Layer 3"
        ohos:height="80vp"
        ohos:width="80vp"
        ohos:background_element="#00BFC9" />
        
</StackLayout>

多个视图排列效果

使用相对位置添加组件

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

xml布局:

<?xml version="1.0" encoding="utf-8"?>
<StackLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:id="$+id:stack_layout"
    ohos:height="match_parent"
    ohos:width="match_parent">

   <Button
        ohos:id="$+id:button"
        ohos:height="40vp"
        ohos:width="80vp"
        ohos:layout_alignment="right"
        ohos:background_element="#3399FF"/>

</StackLayout>

右边布局

场景展示

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

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

 Java示例代码:

ComponentContainer stackLayout = (ComponentContainer) findComponentById(ResourceTable.Id_stack_layout);

Text textFirst = (Text) findComponentById(ResourceTable.Id_text_blue);

textFirst.setClickedListener(new Component.ClickedListener() {
    @Override
    public void onClick(Component component) {
        stackLayout.moveChildToFront(component);
    }
});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值