当前位置: 首页 > news >正文

8.ArkUI Stack的介绍和使用

ArkUI Stack 组件介绍与使用指南

什么是 Stack 组件?

Stack 是 ArkUI 中的层叠布局容器组件,它允许子组件按照添加顺序在 Z 轴方向(垂直于屏幕方向)上堆叠显示。Stack 类似于其他UI框架中的 FrameLayout 或 RelativeLayout,适用于需要重叠显示的UI场景。

Stack 的基本属性

  1. alignContent:设置子组件的对齐方式

    • Alignment.TopStart(默认):左上对齐
    • Alignment.Top:顶部水平居中
    • Alignment.TopEnd:右上对齐
    • Alignment.Start:左侧垂直居中
    • Alignment.Center:居中
    • Alignment.End:右侧垂直居中
    • Alignment.BottomStart:左下对齐
    • Alignment.Bottom:底部水平居中
    • Alignment.BottomEnd:右下对齐
  2. fit:设置 Stack 如何适应子组件大小

    • StackFit.Loose(默认):松散适应,Stack 大小由子组件决定
    • StackFit.Expand:扩展适应,Stack 会填满父容器
    • StackFit.Keep:保持 Stack 原有大小

基本使用方法

@Entry
@Component
struct StackExample {build() {Stack({ alignContent: Alignment.BottomEnd }) {// 背景层Image('background_image').width('100%').height('100%').objectFit(ImageFit.Cover)// 中间层Text('居中文字').fontSize(24).fontColor(Color.White).align(Alignment.Center)// 前景层Button('底部按钮', { type: ButtonType.Capsule }).width(120).height(40).margin(20)}.width('100%').height(300).borderRadius(12).margin(10)}
}

高级用法

绝对定位子组件

Stack() {// 背景Image('background').width('100%').height('100%')// 绝对定位元素Text('绝对定位元素').position({ x: '50%', y: '30%' })  // 相对于Stack定位.translate({ x: -50, y: -15 })     // 偏移自身宽高的一半实现居中.fontSize(20).backgroundColor('#ffffff').padding(10).borderRadius(4)
}
.width(200)
.height(200)
.margin(10)

动态控制层叠顺序

@Entry
@Component
struct DynamicStackExample {@State showOverlay: boolean = falsebuild() {Stack() {// 主内容Column() {Text('主内容区域').fontSize(20).margin(20)Button('切换覆盖层').onClick(() => {this.showOverlay = !this.showOverlay}).margin(20)}.width('100%').height('100%').backgroundColor('#f0f0f0')// 覆盖层if (this.showOverlay) {Column() {Text('覆盖层内容').fontSize(20).margin(20)Button('关闭').onClick(() => {this.showOverlay = false}).margin(20)}.width('80%').height('60%').backgroundColor('#ffffff').align(Alignment.Center).border({ width: 1, color: Color.Grey }).shadow({ radius: 10, color: '#1a000000' })}}.width('100%').height(300).margin(10)}
}

复杂层叠布局

@Entry
@Component
struct ComplexStackLayout {build() {Stack() {// 背景层Image('background').width('100%').height('100%').objectFit(ImageFit.Cover).opacity(0.8)// 中间内容层Column() {Text('标题').fontSize(24).fontWeight(FontWeight.Bold).margin({ bottom: 20 })Text('内容描述...').fontSize(16).margin({ bottom: 20 }).maxLines(3).textOverflow({ overflow: TextOverflow.Ellipsis })Button('操作按钮').width(120)}.width('80%').padding(20).backgroundColor(Color.White).borderRadius(12).align(Alignment.Center).shadow({ radius: 8, color: '#1a000000' })// 右上角标签Text('NEW').fontSize(12).fontColor(Color.White).backgroundColor(Color.Red).padding({ left: 8, right: 8, top: 2, bottom: 2 }).borderRadius(4).position({ x: '85%', y: '15%' })}.width('90%').height(250).margin(10)}
}

实际应用示例

图片标记应用

@Entry
@Component
struct ImageMarker {@State markers: { x: number, y: number, text: string }[] = [{ x: 30, y: 40, text: '标记1' },{ x: 70, y: 60, text: '标记2' }]build() {Stack() {// 基础图片Image('scenery').width('100%').height(300).objectFit(ImageFit.Cover)// 标记点ForEach(this.markers, (marker) => {Column() {Image('marker_icon').width(24).height(24)Text(marker.text).fontSize(12).backgroundColor(Color.White).padding(4).borderRadius(4)}.position({ x: `${marker.x}%`, y: `${marker.y}%` }).onClick(() => {// 处理标记点击事件})})// 添加标记按钮Button('添加标记', { type: ButtonType.Circle }).width(50).height(50).position({ x: '90%', y: '90%' }).onClick(() => {// 添加新标记逻辑})}.width('100%').height(300).margin(10)}
}

浮动操作按钮 (FAB)

@Entry
@Component
struct FabExample {@State showActions: boolean = falsebuild() {Stack() {// 主内容Column() {Text('页面内容').fontSize(20).margin(20)// 更多内容...}.width('100%').height('100%')// 浮动操作按钮Column() {if (this.showActions) {Button('分享', { type: ButtonType.Circle }).width(50).height(50).margin(5).backgroundColor('#4CAF50')Button('收藏', { type: ButtonType.Circle }).width(50).height(50).margin(5).backgroundColor('#2196F3')}Button('+', { type: ButtonType.Circle }).width(60).height(60).backgroundColor('#FF5722').fontSize(24).fontColor(Color.White).onClick(() => {this.showActions = !this.showActions})}.position({ x: '85%', y: '80%' })}.width('100%').height('100%')}
}

注意事项

  1. Stack 的子组件默认会从左上角开始堆叠,可以使用 position 属性进行精确定位
  2. 后添加的子组件会显示在先添加的子组件上方(Z轴顺序)
  3. 使用 position 定位时,百分比值相对于 Stack 的尺寸
  4. 避免在 Stack 中放置过多子组件,可能会影响性能
  5. 对于需要精确控制层叠顺序的场景,可以结合 zIndex 属性使用

Stack 组件特别适合需要重叠显示UI元素的场景,如浮动按钮、对话框、图片标记、卡片式设计等。合理使用 Stack 可以创建出富有层次感的用户界面。

相关文章:

  • 仿真每日一练 | Workbench多单元混合建模静力学分析
  • AI大模型学习十二:‌尝鲜ubuntu 25.04 桌面版私有化sealos cloud + devbox+minio对象存储测试和漫长修改之路
  • 用 24 小时登顶权威榜单的 HiDream-i1-dev,参与“AI 神笔马良挑战赛“
  • commix
  • HTTP状态码
  • HarmonyOS NEXT应用开发-Notification Kit(用户通知服务)notificationManager.addSlot
  • 【差分隐私】假设检验的视角(高斯差分隐私)
  • html+servlet项目中的echart图表
  • 【分布式系统中的“瑞士军刀”_ Zookeeper】一、Zookeeper 快速入门和核心概念
  • 利用TTP协议 ETag + 路由守卫 实现前端发版后通知用户更新得一个方案
  • ​升级Ubuntu 20.04 LTS到22.04 LTS​
  • websheet之 编辑器
  • 开发体育直播系统内容与用户管理机制技术实现方案
  • 【玩泰山派】7、玩linux桌面环境xfce - (4)使用gstreamer
  • 3.4 Spring Boot异常处理
  • 酷狗音乐安卓版K歌功能与音效优化体验测评
  • 基于vue框架的电信用户业务管理系统的设计与实现8ly70(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
  • 容器化实现基于的技术
  • C++----模拟实现string
  • 《Java编程思想》读书笔记:第十章 内部类
  • 新华时评·首季中国经济观察丨用好用足更加积极的财政政策
  • 我驻美使馆:中美并没有就关税问题磋商谈判,更谈不上达成协议
  • 神舟二十号载人飞船与空间站组合体完成自主快速交会对接
  • 时隔七年,上合组织国家电影节再度在中国举办
  • 稀土管制难倒特斯拉人形机器人“擎天柱”,马斯克:“正与中国协商”
  • 教育部增设29种本科新专业,首建战略急需专业超常设置机制