鸿蒙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

相关文章

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

HTML5 搜索框Search Box详解

《HTML5搜索框SearchBox详解》HTML5的搜索框是一个强大的工具,能够有效提升用户体验,通过结合自动补全功能和适当的样式,可以创建出既美观又实用的搜索界面,这篇文章给大家介绍HTML5... html5 搜索框(Search Box)详解搜索框是一个用于输入查询内容的控件,通常用于网站或应用程

Python中使用uv创建环境及原理举例详解

《Python中使用uv创建环境及原理举例详解》uv是Astral团队开发的高性能Python工具,整合包管理、虚拟环境、Python版本控制等功能,:本文主要介绍Python中使用uv创建环境及... 目录一、uv工具简介核心特点:二、安装uv1. 通过pip安装2. 通过脚本安装验证安装:配置镜像源(可

C++ 函数 strftime 和时间格式示例详解

《C++函数strftime和时间格式示例详解》strftime是C/C++标准库中用于格式化日期和时间的函数,定义在ctime头文件中,它将tm结构体中的时间信息转换为指定格式的字符串,是处理... 目录C++ 函数 strftipythonme 详解一、函数原型二、功能描述三、格式字符串说明四、返回值五

LiteFlow轻量级工作流引擎使用示例详解

《LiteFlow轻量级工作流引擎使用示例详解》:本文主要介绍LiteFlow是一个灵活、简洁且轻量的工作流引擎,适合用于中小型项目和微服务架构中的流程编排,本文给大家介绍LiteFlow轻量级工... 目录1. LiteFlow 主要特点2. 工作流定义方式3. LiteFlow 流程示例4. LiteF

CSS3中的字体及相关属性详解

《CSS3中的字体及相关属性详解》:本文主要介绍了CSS3中的字体及相关属性,详细内容请阅读本文,希望能对你有所帮助... 字体网页字体的三个来源:用户机器上安装的字体,放心使用。保存在第三方网站上的字体,例如Typekit和Google,可以link标签链接到你的页面上。保存在你自己Web服务器上的字

MySQL存储过程之循环遍历查询的结果集详解

《MySQL存储过程之循环遍历查询的结果集详解》:本文主要介绍MySQL存储过程之循环遍历查询的结果集,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言1. 表结构2. 存储过程3. 关于存储过程的SQL补充总结前言近来碰到这样一个问题:在生产上导入的数据发现

MyBatis ResultMap 的基本用法示例详解

《MyBatisResultMap的基本用法示例详解》在MyBatis中,resultMap用于定义数据库查询结果到Java对象属性的映射关系,本文给大家介绍MyBatisResultMap的基本... 目录MyBATis 中的 resultMap1. resultMap 的基本语法2. 简单的 resul

华为鸿蒙HarmonyOS 5.1官宣7月开启升级! 首批支持名单公布

《华为鸿蒙HarmonyOS5.1官宣7月开启升级!首批支持名单公布》在刚刚结束的华为Pura80系列及全场景新品发布会上,除了众多新品的发布,还有一个消息也点燃了所有鸿蒙用户的期待,那就是Ha... 在今日的华为 Pura 80 系列及全场景新品发布会上,华为宣布鸿蒙 HarmonyOS 5.1 将于 7