鸿蒙Harmony-层叠布局(Stack)详解

2024-01-15 02:36

本文主要是介绍鸿蒙Harmony-层叠布局(Stack)详解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

我们总是为了太多遥不可及的东西去拼命,却忘了人生真正的幸福不过是灯火阑珊处的温暖,柴米油盐的充实,人生无论你赚的钱,是多还是少,经历的事情是好还是坏,都不如过好当下的每一天! 

目录

一,定义

二,开发布局

三,对齐方式

3.1 TopStart顶部起始端 

3.2 Top顶部横向居中

3.3 TopEnd顶部尾端

3.4 Start起始端纵向居中

3.5 Center横向和纵向居中

3.6 End尾端纵向居中

3.7 BottomStart底部起始端

3.8 Bottom底部横向居中

3.9 BottomEnd底部尾端

四,Z序控制

一,定义

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

层叠布局具有较强的页面层叠、位置定位能力,其使用场景有广告、卡片层叠效果等。

二,开发布局

Stack组件为容器组件,容器内可包含各种子元素。其中子元素默认进行居中堆叠。子元素被约束在Stack下,进行自己的样式定义以及排列。

@Entry
@Component
struct StackTest {build() {Stack(){Column(){}.width('90%').height('90%').backgroundColor('#ff2')Text('text').width('60%').height('60%').backgroundColor('#f23')Button('button').width('30%').height('30%').backgroundColor('#ff8ff3eb').fontColor('#000')}.width('100%').height('100%')}
}

三,对齐方式

3.1 TopStart顶部起始端 

@Entry
@Component
struct StackTest {build() {Stack({alignContent:Alignment.TopStart}){Column(){}.width('90%').height('90%').backgroundColor('#ff2')Text('text').width('60%').height('60%').backgroundColor('#f23')Button('button').width('30%').height('30%').backgroundColor('#ff8ff3eb').fontColor('#000')}.width('100%').height('100%')}
}

3.2 Top顶部横向居中

@Entry
@Component
struct StackTest {build() {Stack({alignContent:Alignment.Top}){Column(){}.width('90%').height('90%').backgroundColor('#ff2')Text('text').width('60%').height('60%').backgroundColor('#f23')Button('button').width('30%').height('30%').backgroundColor('#ff8ff3eb').fontColor('#000')}.width('100%').height('100%')}
}

3.3 TopEnd顶部尾端

@Entry
@Component
struct StackTest {build() {Stack({alignContent:Alignment.TopEnd}){Column(){}.width('90%').height('90%').backgroundColor('#ff2')Text('text').width('60%').height('60%').backgroundColor('#f23')Button('button').width('30%').height('30%').backgroundColor('#ff8ff3eb').fontColor('#000')}.width('100%').height('100%')}
}

3.4 Start起始端纵向居中

@Entry
@Component
struct StackTest {build() {Stack({alignContent:Alignment.Start}){Column(){}.width('90%').height('90%').backgroundColor('#ff2')Text('text').width('60%').height('60%').backgroundColor('#f23')Button('button').width('30%').height('30%').backgroundColor('#ff8ff3eb').fontColor('#000')}.width('100%').height('100%')}
}

3.5 Center横向和纵向居中

@Entry
@Component
struct StackTest {build() {Stack({alignContent:Alignment.Center}){Column(){}.width('90%').height('90%').backgroundColor('#ff2')Text('text').width('60%').height('60%').backgroundColor('#f23')Button('button').width('30%').height('30%').backgroundColor('#ff8ff3eb').fontColor('#000')}.width('100%').height('100%')}
}

3.6 End尾端纵向居中

@Entry
@Component
struct StackTest {build() {Stack({alignContent:Alignment.End}){Column(){}.width('90%').height('90%').backgroundColor('#ff2')Text('text').width('60%').height('60%').backgroundColor('#f23')Button('button').width('30%').height('30%').backgroundColor('#ff8ff3eb').fontColor('#000')}.width('100%').height('100%')}
}

3.7 BottomStart底部起始端

@Entry
@Component
struct StackTest {build() {Stack({alignContent:Alignment.BottomStart}){Column(){}.width('90%').height('90%').backgroundColor('#ff2')Text('text').width('60%').height('60%').backgroundColor('#f23')Button('button').width('30%').height('30%').backgroundColor('#ff8ff3eb').fontColor('#000')}.width('100%').height('100%')}
}

3.8 Bottom底部横向居中

@Entry
@Component
struct StackTest {build() {Stack({alignContent:Alignment.Bottom}){Column(){}.width('90%').height('90%').backgroundColor('#ff2')Text('text').width('60%').height('60%').backgroundColor('#f23')Button('button').width('30%').height('30%').backgroundColor('#ff8ff3eb').fontColor('#000')}.width('100%').height('100%')}
}

3.9 BottomEnd底部尾端

@Entry
@Component
struct StackTest {build() {Stack({alignContent:Alignment.BottomEnd}){Column(){}.width('90%').height('90%').backgroundColor('#ff2')Text('text').width('60%').height('60%').backgroundColor('#f23')Button('button').width('30%').height('30%').backgroundColor('#ff8ff3eb').fontColor('#000')}.width('100%').height('100%')}
}

四,Z序控制

Stack容器中兄弟组件显示层级关系可以通过Z序控制的zIndex属性改变。zIndex值越大,显示层级越高,即zIndex值大的组件会覆盖在zIndex值小的组件上方。

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

@Entry
@Component
struct StackTest {build() {Stack({alignContent:Alignment.Start}){Column() {Text('袁震1').textAlign(TextAlign.End).fontSize(20)}.width(100).height(100).backgroundColor(0xffd306)Column() {Text('袁震2').fontSize(20)}.width(150).height(150).backgroundColor(Color.Pink)Column() {Text('袁震3').fontSize(20)}.width(200).height(200).backgroundColor(Color.Grey)}.width('100%').height('100%')}
}

因为袁震3在最上面,且大于袁震1和袁震2,所以只显示袁震3

然后改变Z序:

@Entry
@Component
struct StackTest {build() {Stack({alignContent:Alignment.Start}){Column() {Text('袁震1').textAlign(TextAlign.End).fontSize(20)}.width(100).height(100).backgroundColor(0xffd306).zIndex(2)Column() {Text('袁震2').fontSize(20)}.width(150).height(150).backgroundColor(Color.Pink).zIndex(1)Column() {Text('袁震3').fontSize(20)}.width(200).height(200).backgroundColor(Color.Grey)}.width('100%').height('100%')}
}

这样就都显示出来了

这篇关于鸿蒙Harmony-层叠布局(Stack)详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/607366

相关文章

MySQL 主从复制部署及验证(示例详解)

《MySQL主从复制部署及验证(示例详解)》本文介绍MySQL主从复制部署步骤及学校管理数据库创建脚本,包含表结构设计、示例数据插入和查询语句,用于验证主从同步功能,感兴趣的朋友一起看看吧... 目录mysql 主从复制部署指南部署步骤1.环境准备2. 主服务器配置3. 创建复制用户4. 获取主服务器状态5

一文详解如何使用Java获取PDF页面信息

《一文详解如何使用Java获取PDF页面信息》了解PDF页面属性是我们在处理文档、内容提取、打印设置或页面重组等任务时不可或缺的一环,下面我们就来看看如何使用Java语言获取这些信息吧... 目录引言一、安装和引入PDF处理库引入依赖二、获取 PDF 页数三、获取页面尺寸(宽高)四、获取页面旋转角度五、判断

Spring Boot中的路径变量示例详解

《SpringBoot中的路径变量示例详解》SpringBoot中PathVariable通过@PathVariable注解实现URL参数与方法参数绑定,支持多参数接收、类型转换、可选参数、默认值及... 目录一. 基本用法与参数映射1.路径定义2.参数绑定&nhttp://www.chinasem.cnbs

MySql基本查询之表的增删查改+聚合函数案例详解

《MySql基本查询之表的增删查改+聚合函数案例详解》本文详解SQL的CURD操作INSERT用于数据插入(单行/多行及冲突处理),SELECT实现数据检索(列选择、条件过滤、排序分页),UPDATE... 目录一、Create1.1 单行数据 + 全列插入1.2 多行数据 + 指定列插入1.3 插入否则更

Redis中Stream详解及应用小结

《Redis中Stream详解及应用小结》RedisStreams是Redis5.0引入的新功能,提供了一种类似于传统消息队列的机制,但具有更高的灵活性和可扩展性,本文给大家介绍Redis中Strea... 目录1. Redis Stream 概述2. Redis Stream 的基本操作2.1. XADD

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

Java JDK1.8 安装和环境配置教程详解

《JavaJDK1.8安装和环境配置教程详解》文章简要介绍了JDK1.8的安装流程,包括官网下载对应系统版本、安装时选择非系统盘路径、配置JAVA_HOME、CLASSPATH和Path环境变量,... 目录1.下载JDK2.安装JDK3.配置环境变量4.检验JDK官网下载地址:Java Downloads

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

MySQL中的LENGTH()函数用法详解与实例分析

《MySQL中的LENGTH()函数用法详解与实例分析》MySQLLENGTH()函数用于计算字符串的字节长度,区别于CHAR_LENGTH()的字符长度,适用于多字节字符集(如UTF-8)的数据验证... 目录1. LENGTH()函数的基本语法2. LENGTH()函数的返回值2.1 示例1:计算字符串

Spring Boot spring-boot-maven-plugin 参数配置详解(最新推荐)

《SpringBootspring-boot-maven-plugin参数配置详解(最新推荐)》文章介绍了SpringBootMaven插件的5个核心目标(repackage、run、start... 目录一 spring-boot-maven-plugin 插件的5个Goals二 应用场景1 重新打包应用