news 2026/10/19 5:40:29

层叠布局 Stack

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
层叠布局 Stack

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

Column(){ Stack({ }) { Column(){}.width('90%').height('100%').backgroundColor('#ff58b87c') Text('text').width('60%').height('60%').backgroundColor('#ffc3f6aa') Button('button').width('30%').height('30%').backgroundColor('#ff8ff3eb').fontColor('#000') // Text('text').width('60%').height('60%').backgroundColor('#ffc3f6aa') 会覆盖Button }.width('100%').height(150).margin({ top: 50 }) }

zIndex

Stack容器中兄弟组件显示层级关系可以通过Z序控制的zIndex属性改变。zIndex值越大,显示层级越高,即zIndex值大的组件会覆盖在zIndex值小的组件上方。一般来说组件的默认层级都是1,可以直接使用一些交大的数去尝试改变层级。类似前端的z-index属性。

注意:

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

Column(){ Stack({ }) { Column(){}.width('90%').height('100%').backgroundColor('#ff58b87c') // Text('text').width('60%').height('60%').backgroundColor('#ffc3f6aa') Button('button').width('30%').height('30%').backgroundColor('#ff8ff3eb').fontColor('#000').zIndex(2) Text('text').width('60%').height('60%').backgroundColor('#ffc3f6aa') // 会覆盖Button }.width('100%').height(150).margin({ top: 50 }) }

对齐方式

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

@Entry @Component struct Index { build() { Column(){ // Stack({ alignContent: Alignment.Start }) { // Stack({ alignContent: Alignment.Center }) { // Stack({ alignContent: Alignment.End }) { // Stack({ alignContent: Alignment.BottomStart }) { // Stack({ alignContent: Alignment.Bottom }) { Stack({ alignContent: Alignment.BottomEnd }) { Column(){}.width('90%').height('100%').backgroundColor('#ff58b87c') // Text('text').width('60%').height('60%').backgroundColor('#ffc3f6aa') Button('button').width('30%').height('30%').backgroundColor('#ff8ff3eb').fontColor('#000').zIndex(2) Text('text').width('60%').height('60%').backgroundColor('#ffc3f6aa') // 会覆盖Button }.width('100%').height(150).margin({ top: 50 }) } } }

offset

可以使用offset调整元素位置

@Entry @Component struct Index { build() { Column(){ Stack({ alignContent: Alignment.BottomEnd }) { Column(){}.width('90%').height('100%').backgroundColor('#ff58b87c') // Text('text').width('60%').height('60%').backgroundColor('#ffc3f6aa') Button('button').width('30%').height('30%').backgroundColor('#ff8ff3eb').fontColor('#000').zIndex(2).offset({x: -10, y: -20}) Text('text').width('60%').height('60%').backgroundColor('#ffc3f6aa') // 会覆盖Button }.width('100%').height(150).margin({ top: 50 }) } } }

返回顶部场景

@Entry @Component struct Index { ​ @State names:number[] = [1,2,3,4,5,6,7,8,9] ​ @State isShow:boolean = false ​ ​ scroll = new Scroller() ​ build() { Column(){ Stack({alignContent:Alignment.BottomEnd}) { ​ if (this.isShow) { Text('↑') .width(80) .height(80) .fontColor(Color.White) .fontSize(24) .backgroundColor(Color.Black) .borderRadius(40) .textAlign(TextAlign.Center) .zIndex(2) .offset({x:-10,y:-20}) .stateStyles({ pressed: { .backgroundColor(Color.Red) } }) .onClick(() => { this.scroll.scrollToIndex(0) }) } ​ List({scroller: this.scroll}) { ForEach(this.names, (item:number) => { ListItem() { Text(item.toString()).width('100%').height(200).backgroundColor(Color.Gray).margin({bottom:20}) } }) } .onScrollIndex((start:number,end:number) => { this.isShow = end > 5 }) // ... } } } }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/10/18 16:38:42

终极Windows日志管理方案:快速搭建专业级监控中心

在复杂的网络环境中,如何高效监控设备日志成为每个系统管理员面临的挑战。Visual Syslog Server作为一款专为Windows平台设计的免费开源工具,通过直观的图形界面和强大的功能组合,让日志管理变得前所未有的简单。本文将带您从零开始&#xff…

作者头像 李华
网站建设 2026/10/19 10:23:39

2025年12月14日全球AI前沿动态

摘要 AI行业动态,全面覆盖模型技术突破、智能体应用、机器人发展、硬件基建、企业动态、行业观点、安全监管等核心领域。全球AI行业呈现技术迭代加速(如GPT-5.2、Gemini系列更新)、应用场景深化(具身智能、行业解决方案落地&#…

作者头像 李华
网站建设 2026/10/18 9:06:03

剪映自动化神器:Python编程实现智能视频剪辑的革命性突破

剪映自动化神器:Python编程实现智能视频剪辑的革命性突破 【免费下载链接】JianYingApi Third Party JianYing Api. 第三方剪映Api 项目地址: https://gitcode.com/gh_mirrors/ji/JianYingApi 还在为重复的视频剪辑工作烦恼吗?剪映自动化神器Jian…

作者头像 李华
网站建设 2026/10/18 6:10:36

终极硬件信息获取指南:5分钟掌握hwinfo跨平台监控技术

终极硬件信息获取指南:5分钟掌握hwinfo跨平台监控技术 【免费下载链接】hwinfo cross platform C library for hardware information (CPU, RAM, GPU, ...) 项目地址: https://gitcode.com/gh_mirrors/hw/hwinfo hwinfo是一款功能强大的跨平台C硬件信息获取库…

作者头像 李华
网站建设 2026/10/19 6:08:04

【Java毕设全套源码+文档】基于springboot的电影院订票信息管理系统设计与实现(丰富项目+远程调试+讲解+定制)

博主介绍:✌️码农一枚 ,专注于大学生项目实战开发、讲解和毕业🚢文撰写修改等。全栈领域优质创作者,博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、小程序技术领域和毕业项目实战 ✌️技术范围:&am…

作者头像 李华