news 2026/6/25 12:20:30

层叠布局 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/6/24 15:42:04

Formily终极指南:5步搞定第三方UI库无缝集成

Formily终极指南:5步搞定第三方UI库无缝集成 【免费下载链接】formily 📱🚀 🧩 Cross Device & High Performance Normal Form/Dynamic(JSON Schema) Form/Form Builder -- Support React/React Native/Vue 2/Vue 3 项目地址…

作者头像 李华
网站建设 2026/6/22 21:36:21

Nugget终极指南:极速下载的完整解决方案

Nugget终极指南:极速下载的完整解决方案 【免费下载链接】nugget minimalist wget clone written in node. HTTP GET files and downloads them into the current directory 项目地址: https://gitcode.com/gh_mirrors/nu/nugget 在当今数据爆炸的时代&#…

作者头像 李华
网站建设 2026/6/21 22:46:12

Zotero-GPT插件API密钥配置全攻略:从入门到精通

Zotero-GPT插件API密钥配置全攻略:从入门到精通 【免费下载链接】zotero-gpt GPT Meet Zotero. 项目地址: https://gitcode.com/gh_mirrors/zo/zotero-gpt 还在为Zotero-GPT插件无法正常工作而烦恼吗?API密钥配置问题可能是罪魁祸首!本…

作者头像 李华
网站建设 2026/6/23 19:18:12

从 proto 到 null:手写 instanceof 揪出对象的“祖宗十八代”

手写 instanceof:从原型链里揪出 "血缘关系"🔍 👨‍👩‍👧‍👦 一、回顾原型、原型对象和原型链 在 JavaScript 的世界里,“原型” 这东西就像个神秘的族谱,藏着对象们的…

作者头像 李华
网站建设 2026/6/25 4:08:34

Windows 7系统下Umi-OCR OCR工具的终极运行指南

还在为Windows 7系统无法顺畅运行Umi-OCR而烦恼吗?🤔 这款免费开源的离线OCR工具凭借其强大的截图识别和批量处理功能,已经成为众多用户的首选。今天,就让我们一起探索如何在你的Win7设备上完美驾驭这款OCR利器! 【免费…

作者头像 李华
网站建设 2026/6/25 2:36:41

poetry 常用命令

以下是 Poetry 的核心命令及其功能说明&#xff0c;按功能分类整理&#xff1a; 一、项目初始化与配置命令说明poetry new <project-name>创建新项目结构&#xff08;含 pyproject.toml、README.rst 等&#xff09;poetry init交互式创建 pyproject.toml 文件poetry vers…

作者头像 李华