news 2026/4/22 5:58:54

层叠布局 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/4/21 9:16:45

终极指南:如何用stl-thumb快速预览3D模型文件

终极指南:如何用stl-thumb快速预览3D模型文件 【免费下载链接】stl-thumb Thumbnail generator for STL files 项目地址: https://gitcode.com/gh_mirrors/st/stl-thumb 你是否曾经面对一堆STL文件时感到困惑?就像在黑暗中摸索,完全不…

作者头像 李华
网站建设 2026/4/18 11:25:09

网盘直链下载助手完整教程:3步快速获取真实下载地址的终极指南

还在为网盘下载速度慢而烦恼吗?网盘直链下载助手这款免费开源的神器,能够将六大主流网盘的分享链接转换为真实的直接下载地址,让你无需安装任何客户端即可实现高速下载体验!🚀 【免费下载链接】baiduyun 油猴脚本 - 一…

作者头像 李华
网站建设 2026/4/18 0:26:38

OpenCore Legacy Patcher:让旧Mac重获新生的终极指南

在苹果生态系统中,设备淘汰速度之快令人咋舌。一台性能尚可的Mac,仅仅因为型号较老,就被官方排除在最新macOS支持之外。但OpenCore Legacy Patcher的出现彻底改变了这一局面,它为那些被遗忘的Mac设备注入了新的活力。 【免费下载链…

作者头像 李华
网站建设 2026/4/17 17:17:00

Windows 11 LTSC恢复应用商店完整指南:3步解决应用安装难题

Windows 11 LTSC版本是企业级操作系统,默认不包含Microsoft Store应用商店,这给需要安装UWP应用的用户带来了诸多不便。本指南将详细介绍如何使用LTSC-Add-MicrosoftStore工具快速恢复应用商店功能,让您轻松安装微信、QQ等只能从商店获取的应…

作者头像 李华
网站建设 2026/4/20 13:22:21

高级专业Terraria地图编辑器深度技术解析

高级专业Terraria地图编辑器深度技术解析 【免费下载链接】Terraria-Map-Editor TEdit - Terraria Map Editor - TEdit is a stand alone, open source map editor for Terraria. It lets you edit maps just like (almost) paint! It also lets you change world settings (ti…

作者头像 李华
网站建设 2026/4/18 12:09:39

Spring Boot 启动流程源码解析:从 `main()` 到 Web 服务就绪

Spring Boot 启动流程源码解析:从 main() 到 Web 服务就绪 一句 SpringApplication.run() 背后,藏着整个 Spring 生态的启动引擎。 你是否曾: 在面试被问:“Spring Boot 启动过程做了哪些事?”遇到启动慢、Bean 找不到…

作者头像 李华