首页/新闻资讯/正文详情

HarmonyOS 6.1 实战:Stack 层叠布局与 Overlay 浮层详解

发布时间:2026/11/28 4:26:07 来源:云帆数科 栏目:资讯中心
HarmonyOS 6.1 实战:Stack 层叠布局与 Overlay 浮层详解
前言在移动应用开发中层叠布局是实现复杂 UI 的核心能力——从头像角标到全屏蒙层从浮动按钮到图片水印都离不开精确的层叠控制。HarmonyOS ArkUI 提供了Stack组件与.overlay()修饰符来应对这类场景。本文通过完整可运行的代码逐一演示它们的核心用法。运行效果初始状态交互后切换对齐 z-index核心 API 概览API作用Stack({ alignContent })子组件全部叠放alignContent控制默认对齐点.zIndex(n)调整单个子组件的层叠顺序数值越大越靠前.overlay(builder, options)在组件上方浮置内容不占布局空间.offset({ x, y })相对自身默认位置平移常与 Stack 配合使用完整示例代码// Stack 层叠布局与 Overlay 浮层 Demo class AlignOption { label: string value: Alignment Alignment.Center } Entry Component struct Index { State alignIndex: number 4 // default Center State zToggle: boolean false State showOverlay: boolean true private alignOptions: AlignOption[] [ { label: TopStart, value: Alignment.TopStart }, { label: Top, value: Alignment.Top }, { label: TopEnd, value: Alignment.TopEnd }, { label: Start, value: Alignment.Start }, { label: Center, value: Alignment.Center }, { label: End, value: Alignment.End }, { label: BottomStart, value: Alignment.BottomStart }, { label: Bottom, value: Alignment.Bottom }, { label: BottomEnd, value: Alignment.BottomEnd }, ] // overlay 内容需要用 Builder 封装不能内联组件表达式 Builder overlayContent() { if (this.showOverlay) { Column() { Text( overlay 叠加层) .fontSize(18) .fontColor(#fff) .fontWeight(FontWeight.Bold) Text(半透明背景效果) .fontSize(12) .fontColor(rgba(255,255,255,0.8)) .margin({ top: 4 }) } .width(100%) .height(100%) .backgroundColor(rgba(0,0,0,0.45)) .justifyContent(FlexAlign.Center) .borderRadius(12) } } Builder overlayBadgeHot() { Text(HOT ) .fontSize(11) .fontColor(#fff) .backgroundColor(#ff3300) .borderRadius(4) .padding({ left: 6, right: 6, top: 2, bottom: 2 }) } Builder overlayBadgeNew() { Text(NEW ✨) .fontSize(11) .fontColor(#fff) .backgroundColor(#0066ff) .borderRadius(4) .padding({ left: 6, right: 6, top: 2, bottom: 2 }) } build() { Stack({ alignContent: Alignment.BottomEnd }) { Scroll() { Column({ space: 0 }) { Text(Stack 层叠布局 Overlay) .fontSize(20) .fontWeight(FontWeight.Bold) .fontColor(#1a1a1a) .width(100%) .padding({ left: 16, right: 16, top: 20, bottom: 8 }) // ① alignContent 九宫格选择器 Text(① alignContent 九宫格对齐) .fontSize(15) .fontWeight(FontWeight.Bold) .fontColor(#333) .width(100%) .padding({ left: 16, top: 12, bottom: 6 }) Column({ space: 4 }) { Text(当前: this.alignOptions[this.alignIndex].label) .fontSize(13).fontColor(#0066ff).margin({ bottom: 8 }) Row({ space: 6 }) { Button(TL).fontSize(11).width(72).height(32) .backgroundColor(this.alignIndex 0 ? #0066ff : #e8e8e8) .fontColor(this.alignIndex 0 ? #fff : #333) .onClick(() { this.alignIndex 0 }) Button(Top).fontSize(11).width(72).height(32) .backgroundColor(this.alignIndex 1 ? #0066ff : #e8e8e8) .fontColor(this.alignIndex 1 ? #fff : #333) .onClick(() { this.alignIndex 1 }) Button(TR).fontSize(11).width(72).height(32) .backgroundColor(this.alignIndex 2 ? #0066ff : #e8e8e8) .fontColor(this.alignIndex 2 ? #fff : #333) .onClick(() { this.alignIndex 2 }) } Row({ space: 6 }) { Button(Start).fontSize(11).width(72).height(32) .backgroundColor(this.alignIndex 3 ? #0066ff : #e8e8e8) .fontColor(this.alignIndex 3 ? #fff : #333) .onClick(() { this.alignIndex 3 }) Button(Center).fontSize(11).width(72).height(32) .backgroundColor(this.alignIndex 4 ? #0066ff : #e8e8e8) .fontColor(this.alignIndex 4 ? #fff : #333) .onClick(() { this.alignIndex 4 }) Button(End).fontSize(11).width(72).height(32) .backgroundColor(this.alignIndex 5 ? #0066ff : #e8e8e8) .fontColor(this.alignIndex 5 ? #fff : #333) .onClick(() { this.alignIndex 5 }) } Row({ space: 6 }) { Button(BL).fontSize(11).width(72).height(32) .backgroundColor(this.alignIndex 6 ? #0066ff : #e8e8e8) .fontColor(this.alignIndex 6 ? #fff : #333) .onClick(() { this.alignIndex 6 }) Button(Bottom).fontSize(11).width(72).height(32) .backgroundColor(this.alignIndex 7 ? #0066ff : #e8e8e8) .fontColor(this.alignIndex 7 ? #fff : #333) .onClick(() { this.alignIndex 7 }) Button(BR).fontSize(11).width(72).height(32) .backgroundColor(this.alignIndex 8 ? #0066ff : #e8e8e8) .fontColor(this.alignIndex 8 ? #fff : #333) .onClick(() { this.alignIndex 8 }) } } .width(100%) .alignItems(HorizontalAlign.Center) .padding({ left: 16, right: 16 }) // Stack 预览 Stack({ alignContent: this.alignOptions[this.alignIndex].value }) { Column() .width(100%).height(180) .backgroundColor(#e8f0ff).borderRadius(12) Column() { Text(this.alignOptions[this.alignIndex].label) .fontSize(14).fontColor(#fff).fontWeight(FontWeight.Bold) } .width(100).height(44) .backgroundColor(#0066ff).borderRadius(8) .justifyContent(FlexAlign.Center) } .width(100%).height(180) .margin({ left: 16, right: 16, top: 12 }) .borderRadius(12).clip(true) // ② z-index 层级 Text(② z-index 层级控制) .fontSize(15).fontWeight(FontWeight.Bold).fontColor(#333) .width(100%).padding({ left: 16, top: 20, bottom: 6 }) Button(this.zToggle ? 恢复默认层级 : 切换 z-index) .fontSize(13).height(36).backgroundColor(#ff6600) .margin({ left: 16, bottom: 10 }) .onClick(() { this.zToggle !this.zToggle }) Stack() { Column() { Text(Box 1).fontSize(12).fontColor(#fff) Text(z (this.zToggle ? 3 : 1)).fontSize(11).fontColor(rgba(255,255,255,0.8)) } .width(140).height(100).backgroundColor(#ff4d4d) .borderRadius(8).justifyContent(FlexAlign.Center) .zIndex(this.zToggle ? 3 : 1).offset({ x: 0, y: 0 }) Column() { Text(Box 2).fontSize(12).fontColor(#fff) Text(z (this.zToggle ? 1 : 2)).fontSize(11).fontColor(rgba(255,255,255,0.8)) } .width(140).height(100).backgroundColor(#33aa66) .borderRadius(8).justifyContent(FlexAlign.Center) .zIndex(this.zToggle ? 1 : 2).offset({ x: 40, y: 20 }) Column() { Text(Box 3).fontSize(12).fontColor(#fff) Text(z (this.zToggle ? 2 : 3)).fontSize(11).fontColor(rgba(255,255,255,0.8)) } .width(140).height(100).backgroundColor(#0066ff) .borderRadius(8).justifyContent(FlexAlign.Center) .zIndex(this.zToggle ? 2 : 3).offset({ x: 80, y: 40 }) } .width(100%).height(160).padding({ left: 16 }) // ③ overlay 叠加层 Text(③ overlay 叠加层) .fontSize(15).fontWeight(FontWeight.Bold).fontColor(#333) .width(100%).padding({ left: 16, top: 20, bottom: 6 }) Button(this.showOverlay ? 隐藏 overlay : 显示 overlay) .fontSize(13).height(36).backgroundColor(#9933cc) .margin({ left: 16, bottom: 10 }) .onClick(() { this.showOverlay !this.showOverlay }) Column() .width(100%).height(150) .backgroundColor(#3366cc).borderRadius(12) .margin({ left: 16, right: 16 }) // overlay 必须传 Builder 引用不能传内联组件 .overlay(this.overlayContent, { align: Alignment.Center }) // ④ 图片卡片角标 Text(④ 图片卡片 overlay 角标) .fontSize(15).fontWeight(FontWeight.Bold).fontColor(#333) .width(100%).padding({ left: 16, top: 20, bottom: 6 }) Row({ space: 10 }) { Column() .width(150).height(100) .backgroundColor(#ff9900).borderRadius(10) .overlay(this.overlayBadgeHot, { align: Alignment.TopEnd }) Column() .width(150).height(100) .backgroundColor(#00aa55).borderRadius(10) .overlay(this.overlayBadgeNew, { align: Alignment.TopStart }) } .padding({ left: 16 }) Column().height(100) } .width(100%) } .scrollable(ScrollDirection.Vertical) .scrollBar(BarState.Auto) .width(100%).height(100%) // 悬浮 FAB利用 Stack alignContent.BottomEnd Stack({ alignContent: Alignment.Center }) { Column() .width(56).height(56) .backgroundColor(#0066ff).borderRadius(28) .shadow({ radius: 8, color: rgba(0,102,255,0.4), offsetX: 0, offsetY: 4 }) Text() .fontSize(28).fontColor(#fff).fontWeight(FontWeight.Bold) .margin({ bottom: 2 }) } .width(56).height(56) .margin({ right: 20, bottom: 20 }) .onClick(() { this.alignIndex (this.alignIndex 1) % 9 }) } .width(100%).height(100%) .backgroundColor(#f8f9fa) } }关键知识点1. overlay() 必须传 Builder 引用.overlay()的类型签名是string | CustomBuilder | ComponentContentObject。不能将内联组件表达式如Column() { ... }结果、Text(...)结果直接传入必须封装为Builder方法再传引用// 错误直接传内联 Column 表达式 .overlay(Column() { Text(hello) }, { align: Alignment.Center }) // 正确用 Builder 封装后传引用 Builder myOverlay() { Column() { Text(hello) } } .overlay(this.myOverlay, { align: Alignment.Center })2. Builder 内可以有 if 判断Builder内允许if/else控制流但不允许 let/const 变量声明。通过State响应式变量 if来控制浮层的显隐Builder overlayContent() { if (this.showOverlay) { Column() { ... } .backgroundColor(rgba(0,0,0,0.45)) } }3. overlay 不占布局空间overlay内容浮于组件之上不影响父容器的布局计算这与在 Stack 中额外增加子组件不同方式占布局空间适合场景Stack 子组件是占用 Stack 尺寸多层交叠时需要 z-index 精确控制overlay否角标、蒙层、水印不影响周围布局4. zIndex 改变渲染顺序默认情况下 Stack 子组件按声明顺序叠放后声明的在上层。.zIndex(n)可打破这一规则Stack() { Column().zIndex(3) // 虽然先声明但层级最高显示在最上面 Column().zIndex(1) Column().zIndex(2) }常见应用场景场景实现方式头像右下角角标overlay(badgeBuilder, { align: Alignment.BottomEnd })全屏蒙层overlay(maskBuilder) 宽高 100%backgroundColor半透明悬浮操作按钮Stack 按钮子组件 alignContent: Alignment.BottomEnd图片水印overlay(watermarkBuilder, { align: Alignment.BottomStart })卡片角标HOT/NEWoverlay(badgeBuilder, { align: Alignment.TopEnd })小结Stack让所有子组件叠放在同一区域alignContent控制默认对齐点.zIndex()控制层叠顺序.overlay()是轻量浮层方案不占布局空间内容必须以Builder引用传入Builder方法内允许if/else但不允许let/const变量声明悬浮 FAB 直接利用最外层Stack的alignContent.BottomEnd无需额外定位

相关推荐

HarmonyOS ArkUI RelativeContainer 相对定位布局:ID 锚点与 alignRules 实战
HarmonyOS ArkUI RelativeContainer 相对定位布局:ID 锚点与 alignRules 实战

系列:鸿蒙 HarmonyOS 6.1 新特性实战 第 50 篇 当 Column/Row 的多层嵌套使代码深度超过 4 层,或者需要将某个元素同时相对多个兄弟元素进行对齐时,RelativeContainer 是更优的选择。它通过给每个元素分配 .id() 并在 alignRules 中声明锚点… · 2026/10/25 6:17:44

湿疹管理的三大核心策略与常见误区解析
湿疹管理的三大核心策略与常见误区解析

1. 湿疹管理的核心认知误区湿疹作为一种常见的皮肤问题,困扰着各个年龄段的人群。在门诊工作中,我经常遇到患者陷入两个极端:要么过度依赖激素药膏形成"药膏恐惧症",要么完全拒绝药物治疗导致症状加重。实际上&#xff… · 2026/11/2 0:56:23

AI驱动物理仿真:强化学习实现反重力控制与PyBullet实践
AI驱动物理仿真:强化学习实现反重力控制与PyBullet实践

1. 项目概述:当香蕉遇上反重力最近在物理仿真和AI结合的圈子里,有个项目标题让我眼前一亮:“Nano-Banana实现Antigravity效果”。乍一看,这组合有点天马行空——纳米尺度的香蕉,去实现反重力?但仔细琢磨&am… · 2026/10/26 5:13:29

江苏省电工证技能补贴在哪考最靠谱?怕考不过白交培训费看这篇
江苏省电工证技能补贴在哪考最靠谱?怕考不过白交培训费看这篇

江苏省电工证技能补贴在哪考最靠谱?怕考不过白交培训费看这篇 怕考不过白交培训费?别急,江苏省电工证技能补贴政策正在落实,关键是要 在哪考… · 2026/11/28 4:25:32

特种工考试哪里查询别踩坑 这些细节你得知道
特种工考试哪里查询别踩坑 这些细节你得知道

特种工考试哪里查询别踩坑 这些细节你得知道 之前考的证在外省能不能转过来?这几乎是每个特种作业持证人都会遇到的问题。 千万别踩坑… · 2026/11/28 4:25:31

盐津电工证在哪办多久拿证?实操考试心里没底怕挂科怎么破
盐津电工证在哪办多久拿证?实操考试心里没底怕挂科怎么破

盐津电工证在哪办多久拿证?实操考试心里没底怕挂科怎么破 实操考试心里没底怕挂科,这几乎是每个准备考电工证的人最真实的心理状态。尤其是对新手来说,不知道从哪里开始准备,担心自己操作不熟练,怕挂科,还纠结“ 多久拿证… · 2026/11/28 4:24:31

在郑州可以办电工证吗保姆级教程教你避开中介坑
在郑州可以办电工证吗保姆级教程教你避开中介坑

在郑州可以办电工证吗保姆级教程教你避开中介坑 不知道去哪报名怕被中介坑?在郑州可以办电工证吗,这个问题困扰着不少想转行或者提升技能的打工族。作为在安阳转岗多年的电工从业者,我深知这个证书对咱们一线工人的意义。今天这波保姆级教程,帮你从零开始… · 2026/11/28 4:24:31

电工证机读卡怎么考?真实经历告诉你别被中介骗
电工证机读卡怎么考?真实经历告诉你别被中介骗

电工证机读卡怎么考?真实经历告诉你别被中介骗 你是不是也遇到过这种情况:想考电工证,但不知道去哪儿报名,怕被中介骗?我身边就有人因为没搞清楚流程,花了冤枉钱还拿不到证。今天我就从 电工证机读卡 这个关键词出发,结合 真实经历… · 2026/11/28 4:24:31

低压电工证地线接法在哪考?学历不高也能轻松拿下
低压电工证地线接法在哪考?学历不高也能轻松拿下

低压电工证地线接法在哪考?学历不高也能轻松拿下 你是不是也担心自己学历不够,根本 报不上低压电工证地线接法 的名?其实不然,很多人误以为学历是门槛,但根据 河南省应急管理厅… · 2026/11/28 4:24:31

平价的电工证查询保姆级教程:别被中介坑,一步步教你搞定
平价的电工证查询保姆级教程:别被中介坑,一步步教你搞定

平价的电工证查询保姆级教程:别被中介坑,一步步教你搞定 你是不是也经常在网上看到“平价的电工证查询”这样的关键词,但又不知道去哪报名,怕被中介坑?特别是像你这样的平顶山学员,既想考个正规电工证,又怕花冤枉钱走弯路。别担心,这篇保姆级教程就是… · 2026/11/28 1:08:19

麻阳考电工证在哪里考内部消息你知道吗
麻阳考电工证在哪里考内部消息你知道吗

麻阳考电工证在哪里考内部消息你知道吗 不知道去哪报名怕被中介坑, 内部消息 告诉你,麻阳考电工证不一定要去外地,本地其实也有正规渠道,但很多人因为不了解流程和资质,反而被坑了钱还考不到证。今天我就从一个干了这么多年特种作业培训的“老江湖”角… · 2026/11/27 3:38:27

揭阳普宁电工证换证怎么操作?良心建议帮你理清流程
揭阳普宁电工证换证怎么操作?良心建议帮你理清流程

揭阳普宁电工证换证怎么操作?良心建议帮你理清流程 之前考的证在外省能不能转过来?这是很多已经在外地工作、现在想回揭阳普宁发展的电工朋友最关心的问题。如果你也是其中之一,那这篇文章就是为你们量身定制的,从换证流程到避坑建议,良心建议全给你讲透… · 2026/11/27 17:56:55

电工证在西安哪里办?外省证能转过来吗?上岗必备全流程详解
电工证在西安哪里办?外省证能转过来吗?上岗必备全流程详解

电工证在西安哪里办?外省证能转过来吗?上岗必备全流程详解 之前考的证在外省能不能转过来?这是很多从外地来西安工作的电工最关心的问题。特别是那些在郑州、洛阳、开封等地从事房建工程的电工,随着项目转移到西安,证件问题成了摆在眼前的“上岗必备”难… · 2026/11/28 0:00:19

广东考电工证别再踩坑了!良心建议帮你搞懂报考流程和职业前景
广东考电工证别再踩坑了!良心建议帮你搞懂报考流程和职业前景

广东考电工证别再踩坑了!良心建议帮你搞懂报考流程和职业前景 电工证不光是上岗的通行证,更是你进入工地、工厂、维修行业的“敲门砖”。很多小伙伴都遇到过这种情况:刚拿到证就过期了,换证流程搞不清楚,耽误了工作机会。别急,这篇文章就是给你一个… · 2026/11/28 0:00:19

工地太忙根本没时间复习考试?三方电工证合作机构值不值得考
工地太忙根本没时间复习考试?三方电工证合作机构值不值得考

工地太忙根本没时间复习考试?三方电工证合作机构值不值得考 你是不是也这样,整天在工地上跑来跑去,连吃饭的时间都紧张,更别提抽空复习考试了?尤其是像 三方电工证合作机构 这类证书,听起来门槛不低,但你可能还在纠结—— 值不值得考… · 2026/11/28 0:00:19

双极步进电机控制实战:DRV8818PWPR与PIC18F86K22驱动调试
双极步进电机控制实战:DRV8818PWPR与PIC18F86K22驱动调试

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views … · 2026/11/28 0:39:37

STM32F207ZG + DRV8818PWPR 步进电机驱动实战解析
STM32F207ZG + DRV8818PWPR 步进电机驱动实战解析

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views … · 2026/11/27 17:16:48

DRV8818PWPR+dsPIC30F4013工业级双极步进电机控制方案
DRV8818PWPR+dsPIC30F4013工业级双极步进电机控制方案

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views … · 2026/11/27 17:26:35

了解更多?预约专属演示

我们的顾问将为您一对一讲解产品与方案

企业微信二维码