鸿蒙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作为最流行的开源关系型数据库管理系统之一,提供了多种方式来实现高可用性,其中双机热备(M... 目录1. 环境准备1.1 安装mysql1.2 配置MySQL1.2.1 主服务器配置1.2.2 从

Linux kill正在执行的后台任务 kill进程组使用详解

《Linuxkill正在执行的后台任务kill进程组使用详解》文章介绍了两个脚本的功能和区别,以及执行这些脚本时遇到的进程管理问题,通过查看进程树、使用`kill`命令和`lsof`命令,分析了子... 目录零. 用到的命令一. 待执行的脚本二. 执行含子进程的脚本,并kill2.1 进程查看2.2 遇到的

MyBatis常用XML语法详解

《MyBatis常用XML语法详解》文章介绍了MyBatis常用XML语法,包括结果映射、查询语句、插入语句、更新语句、删除语句、动态SQL标签以及ehcache.xml文件的使用,感兴趣的朋友跟随小... 目录1、定义结果映射2、查询语句3、插入语句4、更新语句5、删除语句6、动态 SQL 标签7、ehc

详解SpringBoot+Ehcache使用示例

《详解SpringBoot+Ehcache使用示例》本文介绍了SpringBoot中配置Ehcache、自定义get/set方式,并实际使用缓存的过程,文中通过示例代码介绍的非常详细,对大家的学习或者... 目录摘要概念内存与磁盘持久化存储:配置灵活性:编码示例引入依赖:配置ehcache.XML文件:配置

从基础到高级详解Go语言中错误处理的实践指南

《从基础到高级详解Go语言中错误处理的实践指南》Go语言采用了一种独特而明确的错误处理哲学,与其他主流编程语言形成鲜明对比,本文将为大家详细介绍Go语言中错误处理详细方法,希望对大家有所帮助... 目录1 Go 错误处理哲学与核心机制1.1 错误接口设计1.2 错误与异常的区别2 错误创建与检查2.1 基础

k8s按需创建PV和使用PVC详解

《k8s按需创建PV和使用PVC详解》Kubernetes中,PV和PVC用于管理持久存储,StorageClass实现动态PV分配,PVC声明存储需求并绑定PV,通过kubectl验证状态,注意回收... 目录1.按需创建 PV(使用 StorageClass)创建 StorageClass2.创建 PV

Python版本信息获取方法详解与实战

《Python版本信息获取方法详解与实战》在Python开发中,获取Python版本号是调试、兼容性检查和版本控制的重要基础操作,本文详细介绍了如何使用sys和platform模块获取Python的主... 目录1. python版本号获取基础2. 使用sys模块获取版本信息2.1 sys模块概述2.1.1

一文详解Python如何开发游戏

《一文详解Python如何开发游戏》Python是一种非常流行的编程语言,也可以用来开发游戏模组,:本文主要介绍Python如何开发游戏的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录一、python简介二、Python 开发 2D 游戏的优劣势优势缺点三、Python 开发 3D

Redis 基本数据类型和使用详解

《Redis基本数据类型和使用详解》String是Redis最基本的数据类型,一个键对应一个值,它的功能十分强大,可以存储字符串、整数、浮点数等多种数据格式,本文给大家介绍Redis基本数据类型和... 目录一、Redis 入门介绍二、Redis 的五大基本数据类型2.1 String 类型2.2 Hash

Java中的.close()举例详解

《Java中的.close()举例详解》.close()方法只适用于通过window.open()打开的弹出窗口,对于浏览器的主窗口,如果没有得到用户允许是不能关闭的,:本文主要介绍Java中的.... 目录当你遇到以下三种情况时,一定要记得使用 .close():用法作用举例如何判断代码中的 input