2.2、层叠布局(Stack)

概述

层叠布局(StackLayout )用于在屏幕上预留一块区域来显示组件中的元素,提供元素可以重叠的布局。层叠布局通过 Stack 容器组件实现位置的固定定位与层叠,容器中的子元素(子组件)依次入栈,后一个子元素覆盖前一个子元素,子元素可以叠加,也可以设置位置。

层叠布局具有较强的页面层叠、位置定位能力,其使用场景有广告、卡片层叠效果等。

我开发的 Demo 展示

以下代码均经过我 demo 的实战验证,确保代码和效果对应

布局

Stack 组件为容器组件,容器内可包含各种子组件。其中子组件默认进行居中堆叠。子元素被约束在 Stack 下,进行自己的样式定义以及排列。

对齐方式

Stack 组件通过 alignContent 参数实现位置的相对移动

Alignment.TopStart

对应代码

ts 复制代码
 Stack({ }) {
   Text('Item3')
     .width('100%')
     .height('100%')
     .backgroundColor('#ff58b87c')
     .align(Alignment.Bottom)

   Text('Item2')
     .width('60%')
     .height('60%')
     .backgroundColor('#ffc3f6aa')
     .align(Alignment.Bottom)

   Text('Item1')
     .width('30%')
     .height('30%')
     .backgroundColor('#ff8ff3eb')
     .fontColor('#000')
 }
 .width('100%')
 .height(150)
 .margin({ top: 10 })
 .alignContent(Alignment.TopStart)

Alignment.Top

对应代码

ts 复制代码
 Stack({ }) {
  Text('Item3')
     .width('100%')
     .height('100%')
     .backgroundColor('#ff58b87c')
     .align(Alignment.Bottom)

   Text('Item2')
     .width('60%')
     .height('60%')
     .backgroundColor('#ffc3f6aa')
     .align(Alignment.Bottom)

   Text('Item1')
     .width('30%')
     .height('30%')
     .backgroundColor('#ff8ff3eb')
     .fontColor('#000')
 }
 .width('100%')
 .height(150)
 .margin({ top: 10 })
 .alignContent(Alignment.Top)

Alignment.TopEnd

对应代码

ts 复制代码
 Stack({ }) {
  Text('Item3')
    .width('100%')
    .height('100%')
    .backgroundColor('#ff58b87c')
    .align(Alignment.Bottom)

  Text('Item2')
    .width('60%')
    .height('60%')
    .backgroundColor('#ffc3f6aa')
    .align(Alignment.Bottom)

  Text('Item1')
    .width('30%')
    .height('30%')
    .backgroundColor('#ff8ff3eb')
    .fontColor('#000')
}
.width('100%')
.height(150)
.margin({ top: 10 })
.alignContent(Alignment.TopEnd)

Alignment.Center

ts 复制代码
Stack({ }) {
 Text('Item3')
    .width('100%')
    .height('100%')
    .backgroundColor('#ff58b87c')
    .align(Alignment.Bottom)

  Text('Item2')
    .width('60%')
    .height('60%')
    .backgroundColor('#ffc3f6aa')
    .align(Alignment.Bottom)

  Text('Item1')
    .width('30%')
    .height('30%')
    .backgroundColor('#ff8ff3eb')
    .fontColor('#000')
}
.width('100%')
.height(150)
.margin({ top: 10 })
.alignContent(Alignment.Center)

Alignment.BottomStart

对应代码

ts 复制代码
Stack({ }) {
  Text('Item3')
     .width('100%')
     .height('100%')
     .backgroundColor('#ff58b87c')
     .align(Alignment.TopStart)

   Text('Item2')
     .width('60%')
     .height('60%')
     .backgroundColor('#ffc3f6aa')
     .align(Alignment.TopStart)

   Text('Item1')
     .width('30%')
     .height('30%')
     .backgroundColor('#ff8ff3eb')
     .fontColor('#000')
 }
 .width('100%')
 .height(150)
 .margin({ top: 10 })
 .alignContent(Alignment.BottomStart)

Alignment.Bottom

对应代码

ts 复制代码
Stack({ }) {
  Text('Item3')
    .width('100%')
    .height('100%')
    .backgroundColor('#ff58b87c')
    .align(Alignment.TopStart)

  Text('Item2')
    .width('60%')
    .height('60%')
    .backgroundColor('#ffc3f6aa')
    .align(Alignment.TopStart)

  Text('Item1')
    .width('30%')
    .height('30%')
    .backgroundColor('#ff8ff3eb')
    .fontColor('#000')
}
.width('100%')
.height(150)
.margin({ top: 10 })
.alignContent(Alignment.Bottom)

Alignment.BottomEnd

对应代码

ts 复制代码
Stack({ }) {
  Text('Item3')
    .width('100%')
    .height('100%')
    .backgroundColor('#ff58b87c')
    .align(Alignment.TopStart)

  Text('Item2')
    .width('60%')
    .height('60%')
    .backgroundColor('#ffc3f6aa')
    .align(Alignment.TopStart)

  Text('Item1')
    .width('30%')
    .height('30%')
    .backgroundColor('#ff8ff3eb')
    .fontColor('#000')
}
.width('100%')
.height(150)
.margin({ top: 10 })
.alignContent(Alignment.BottomEnd)

Z序控制

Stack 容器中兄弟组件显示层级关系可以通过Z序控制的 zIndex 属性改变。zIndex 值越大,显示层级越高,即 zIndex 值大的组件会覆盖在 zIndex 值小的组件上方。

在层叠布局中,如果后面子元素尺寸大于前面子元素尺寸,则前面子元素完全隐藏。

ts 复制代码
Stack({ alignContent: Alignment.BottomStart }) {
  Column() {
    Text('Stack子元素1')
      .textAlign(TextAlign.End)
      .fontSize(20)
  }
  .width(100)
  .height(100)
  .backgroundColor(0xffd306)

  Column() {
    Text('Stack子元素2')
      .fontSize(20)
  }
  .width(150)
  .height(150)
  .backgroundColor(Color.Pink)

  Column() {
    Text('Stack子元素3')
      .fontSize(20)
  }
  .width(200)
  .height(200)
  .backgroundColor(Color.Grey)
}
.margin({ top: 10 })
.width(350)
.height(350)
.backgroundColor(0xe0e0e0)

上图中,最后的子元素3的尺寸大于前面的所有子元素,所以,前面两个元素完全隐藏。改变子元素1,子元素2的zIndex属性后,可以将元素展示出来。 对应代码

ts 复制代码
Stack({ alignContent: Alignment.BottomStart }) {
  Column() {
    Text('Stack子元素1')
      .textAlign(TextAlign.End)
      .fontSize(20)
  }
  .width(100)
  .height(100)
  .backgroundColor(0xffd306)
  .zIndex(2)

  Column() {
    Text('Stack子元素2')
      .fontSize(20)
  }
  .width(150)
  .height(150)
  .backgroundColor(Color.Pink)
  .zIndex(1)

  Column() {
    Text('Stack子元素3')
      .fontSize(20)
  }
  .width(200)
  .height(200)
  .backgroundColor(Color.Grey)
}
.margin({ top: 10 })
.width(350)
.height(350)
.backgroundColor(0xe0e0e0)

上一篇:2.1、线性布局(Row/Column) 下一篇:2.3、弹性布局(Flex)

相关推荐
爱笑的眼睛1111 分钟前
HarmonyOS中Radio单选框组件的交互设计深度解析
华为·harmonyos
yuanlaile4 小时前
Flutter开发HarmonyOS鸿蒙App商业项目实战已出炉
flutter·华为·harmonyos
cooldream20095 小时前
【案例实战】智能出行导航助手HarmonyOS 开发全流程复盘
华为·harmonyos
CodeCaptain5 小时前
可直接落地的「Flutter 桥接鸿蒙 WebSocket」端到端实施方案
websocket·flutter·harmonyos
猫林老师5 小时前
HarmonyOS图形图像处理与OpenGL ES实战
harmonyos
白鹿第一帅5 小时前
【成长纪实】星光不负 码向未来|我的 HarmonyOS 学习之路与社区成长故事
harmonyos·白鹿第一帅·成都ug社区·csdn成都站·鸿蒙开放能力·鸿蒙学习之路·鸿蒙第一课
俩毛豆5 小时前
【页面路由导航】三步实现页面跳转的完整示例
前端·harmonyos
羑悻的小杀马特7 小时前
探秘仓颉:当函数式编程遇见面向对象王国,当协程风暴席卷并发荒原——从基础语法到实战测试联动的多维编程奇遇记
华为·harmonyos·仓颉·仓颉征文·个人感受·标准库源码·语法剖析
LucianaiB8 小时前
【案例实战】基于分布式能力的跨设备任务协同应用开发
harmonyos·鸿蒙·1024程序员节·案例实战
摘星编程21 小时前
【成长纪实】HarmonyOS Next学习地图:新手避坑指南与核心知识点拆解
学习·华为·harmonyos·鸿蒙开发