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

层叠布局 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 }) // ... } } } }
http://www.cnnetsun.cn/news/54593.html

相关文章:

  • 终极Windows日志管理方案:快速搭建专业级监控中心
  • 2025年12月14日全球AI前沿动态
  • 剪映自动化神器:Python编程实现智能视频剪辑的革命性突破
  • 终极硬件信息获取指南:5分钟掌握hwinfo跨平台监控技术
  • MacOS鼠标滚动终极优化:如何让外接鼠标体验超越触控板?[特殊字符]
  • 【Java毕设全套源码+文档】基于springboot的电影院订票信息管理系统设计与实现(丰富项目+远程调试+讲解+定制)
  • Unity反向遮罩黑科技:彻底颠覆传统UI设计思维
  • 3大技巧玩转Equalizer APO:从入门到精通的音频增强终极指南
  • 10分钟精通AMD处理器调试:SMU工具的完整使用指南
  • GridPlayer终极指南:轻松实现多视频同步播放的免费神器
  • 如何用OneMore插件实现终极笔记管理:开源免费的效率神器
  • Bilibili-Evolved终极评测:从基础体验到专业玩家的全面升级指南
  • ubuntu常用命令
  • wvp-GB28181-pro国标视频平台完整使用教程:从零搭建到实战应用
  • 终极指南:LXMusic音源全面解析与使用技巧
  • GridPlayer:免费开源的多视频网格播放器终极解决方案
  • Terraria地图编辑器终极使用指南:5步轻松打造完美游戏世界
  • iStore插件中心终极指南:5步轻松搞定OpenWRT插件安装与管理
  • OneMore插件:让OneNote效率提升10倍的神器
  • 终极指南:快速上手轻量级文本转语音引擎eSpeak NG
  • 云顶之弈智能挂机助手:解放双手的自动化游戏神器
  • 图像分割标注转换:从入门到精通的全方位指南
  • 3步精通Draw.io Mermaid插件:小白也能快速上手的文本转图表神器
  • 12.14周报
  • 3分钟掌握Easy-Scraper:零基础网页数据抓取神器
  • 飞书文档批量导出终极指南:告别手动烦恼,实现高效迁移
  • SuperCom串口调试工具:终极免费解决方案与5分钟快速部署指南
  • Vulkan显存检测利器:memtest_vulkan快速验证显卡稳定性
  • 揭秘BlenderGIS:5分钟搞定专业级地形生成的秘密武器
  • 终极Mac桌面歌词神器LyricsX完整使用指南