层叠布局(Stack)

news2024/9/23 3:23:28

目录

1、概述

2、开发布局

3、对齐方式

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 

4、Z序控制

5、场景示例


1、概述

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

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

        如图1,Stack作为容器,容器内的子元素(子组件)的顺序为Item1->Item2->Item3。

图1 层叠布局

2、开发布局

        Stack组件为容器组件,容器内可包含各种子组件。其中子组件默认进行居中堆叠。子元素被约束在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')
  }.width('100%').height(150).margin({ top: 50 })
}

3、对齐方式

        Stack组件通过alignContent参数实现位置的相对移动。如图2所示,支持九种对齐方式。

图2 Stack容器内元素的对齐方式

3.1、TopStart 

        顶部向左对齐。

@Entry
@Component
struct StackAlignContentPage {
  @State message: string = 'Hello World'

  build() {
    Stack({ alignContent: Alignment.TopStart }) {
      Column() {
      }.width(200).height(200).backgroundColor(0x86C5E3)

      Column() {
      }.width(150).height(150).backgroundColor(0x92D6CC)

      Column() {
      }.width(100).height(100).backgroundColor(0xF5DC62)
    }.margin({ top: 100 })
    .width(350)
    .height(350)
    .backgroundColor(0xe0e0e0)
  }
}

 

3.2、Top

        顶部居中对齐。

@Entry
@Component
struct StackAlignContentPage {
  @State message: string = 'Hello World'

  build() {
    Stack({ alignContent: Alignment.Top }) {
      Column() {
      }.width(200).height(200).backgroundColor(0x86C5E3)

      Column() {
      }.width(150).height(150).backgroundColor(0x92D6CC)

      Column() {
      }.width(100).height(100).backgroundColor(0xF5DC62)
    }.margin({ top: 100 })
    .width(350)
    .height(350)
    .backgroundColor(0xe0e0e0)
  }
}

 

3.3、TopEnd

        顶部向右对齐。

@Entry
@Component
struct StackAlignContentPage {
  @State message: string = 'Hello World'

  build() {
    Stack({ alignContent: Alignment.TopEnd }) {
      Column() {
      }.width(200).height(200).backgroundColor(0x86C5E3)

      Column() {
      }.width(150).height(150).backgroundColor(0x92D6CC)

      Column() {
      }.width(100).height(100).backgroundColor(0xF5DC62)
    }.margin({ top: 100 })
    .width(350)
    .height(350)
    .backgroundColor(0xe0e0e0)
  }
}

3.4、Start

        竖直居中、横向居左对齐。

@Entry
@Component
struct StackAlignContentPage {
  @State message: string = 'Hello World'

  build() {
    Stack({ alignContent: Alignment.Start }) {
      Column() {
      }.width(200).height(200).backgroundColor(0x86C5E3)

      Column() {
      }.width(150).height(150).backgroundColor(0x92D6CC)

      Column() {
      }.width(100).height(100).backgroundColor(0xF5DC62)
    }.margin({ top: 100 })
    .width(350)
    .height(350)
    .backgroundColor(0xe0e0e0)
  }
}

3.5、Center

        竖直居中、横向居中对齐。

@Entry
@Component
struct StackAlignContentPage {
  @State message: string = 'Hello World'

  build() {
    Stack({ alignContent: Alignment.Center }) {
      Column() {
      }.width(200).height(200).backgroundColor(0x86C5E3)

      Column() {
      }.width(150).height(150).backgroundColor(0x92D6CC)

      Column() {
      }.width(100).height(100).backgroundColor(0xF5DC62)
    }.margin({ top: 100 })
    .width(350)
    .height(350)
    .backgroundColor(0xe0e0e0)
  }
}

3.6、End

        竖直居中、横向居右对齐。

@Entry
@Component
struct StackAlignContentPage {
  @State message: string = 'Hello World'

  build() {
    Stack({ alignContent: Alignment.End }) {
      Column() {
      }.width(200).height(200).backgroundColor(0x86C5E3)

      Column() {
      }.width(150).height(150).backgroundColor(0x92D6CC)

      Column() {
      }.width(100).height(100).backgroundColor(0xF5DC62)
    }.margin({ top: 100 })
    .width(350)
    .height(350)
    .backgroundColor(0xe0e0e0)
  }
}

3.7、BottomStart

        底部向左对齐。

@Entry
@Component
struct StackAlignContentPage {
  @State message: string = 'Hello World'

  build() {
    Stack({ alignContent: Alignment.BottomStart }) {
      Column() {
      }.width(200).height(200).backgroundColor(0x86C5E3)

      Column() {
      }.width(150).height(150).backgroundColor(0x92D6CC)

      Column() {
      }.width(100).height(100).backgroundColor(0xF5DC62)
    }.margin({ top: 100 })
    .width(350)
    .height(350)
    .backgroundColor(0xe0e0e0)
  }
}

3.8、Bottom

        底部居中对齐。

@Entry
@Component
struct StackAlignContentPage {
  @State message: string = 'Hello World'

  build() {
    Stack({ alignContent: Alignment.Bottom }) {
      Column() {
      }.width(200).height(200).backgroundColor(0x86C5E3)

      Column() {
      }.width(150).height(150).backgroundColor(0x92D6CC)

      Column() {
      }.width(100).height(100).backgroundColor(0xF5DC62)
    }.margin({ top: 100 })
    .width(350)
    .height(350)
    .backgroundColor(0xe0e0e0)
  }
}

3.9、BottomEnd 

        底部向右对齐。

@Entry
@Component
struct StackAlignContentPage {
  @State message: string = 'Hello World'

  build() {
    Stack({ alignContent: Alignment.BottomEnd }) {
      Column() {
      }.width(200).height(200).backgroundColor(0x86C5E3)

      Column() {
      }.width(150).height(150).backgroundColor(0x92D6CC)

      Column() {
      }.width(100).height(100).backgroundColor(0xF5DC62)
    }.margin({ top: 100 })
    .width(350)
    .height(350)
    .backgroundColor(0xe0e0e0)
  }
}

4、Z序控制

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

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

Stack({ alignContent: Alignment.BottomStart }) {
  Column() {
    Text('Stack子元素1').textAlign(TextAlign.End).fontSize(20)
  }.width(100).height(100).backgroundColor(0xffd306)

  Column() {
    Text('Stack子元素2').fontSize(20)
  }.width(150).height(150).backgroundColor(Color.Pink)

  Column() {
    Text('Stack子元素3').fontSize(20)
  }.width(200).height(200).backgroundColor(Color.Grey)
}.margin({ top: 100 }).width(350).height(350).backgroundColor(0xe0e0e0)

        下图中,最后的子元素3的尺寸大于前面的所有子元素,所以,前面两个元素完全隐藏。改变子元素1,子元素2的zIndex属性后,可以将元素展示出来。

Stack({ alignContent: Alignment.BottomStart }) {
  Column() {
    Text('Stack子元素1').fontSize(20)
  }.width(100).height(100).backgroundColor(0xffd306).zIndex(2)

  Column() {
    Text('Stack子元素2').fontSize(20)
  }.width(150).height(150).backgroundColor(Color.Pink).zIndex(1)

  Column() {
    Text('Stack子元素3').fontSize(20)
  }.width(200).height(200).backgroundColor(Color.Grey)
}.margin({ top: 100 }).width(350).height(350).backgroundColor(0xe0e0e0)

5、场景示例

        使用层叠布局快速搭建手机页面显示模型。

@Entry
@Component
struct StackSample {
  private arr: string[] = ['APP1', 'APP2', 'APP3', 'APP4', 'APP5', 'APP6', 'APP7', 'APP8'];

  build() {
    Stack({ alignContent: Alignment.Bottom }) {
      Flex({ wrap: FlexWrap.Wrap }) {
        ForEach(this.arr, (item) => {
          Text(item)
            .width(100)
            .height(100)
            .fontSize(16)
            .margin(10)
            .textAlign(TextAlign.Center)
            .borderRadius(10)
            .backgroundColor(0xFFFFFF)
        }, item => item)
      }.width('100%').height('100%')

      Flex({ justifyContent: FlexAlign.SpaceAround, alignItems: ItemAlign.Center }) {
        Text('联系人').fontSize(16)
        Text('设置').fontSize(16)
        Text('短信').fontSize(16)
      }
      .width('50%')
      .height(50)
      .backgroundColor('#16302e2e')
      .margin({ bottom: 15 })
      .borderRadius(15)
    }.width('100%').height('100%').backgroundColor('#CFD0CF')
  }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1381062.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Linux的SSH服务

一.SSH服务简介 1.什么是SSH SSH(Secure Shell)是一种安全通道协议,主要用来实现字符界面的远程登录、远程复制等功能。SSH 协议对通信双方的数据传输进行了加密处理,其中包括用户登录时输入的用户口令,SSH 为建立在应…

Elasticsearch:Search tutorial - 使用 Python 进行搜索 (四)

在本节中,你将了解另一种机器学习搜索方法,该方法利用 Elastic Learned Sparse EncodeR 模型或 ELSER,这是一种由 Elastic 训练来执行语义搜索的自然语言处理模型。这是继之前的文章 “Elasticsearch:Search tutorial - 使用 Pyth…

NeRF算法模型简析:从理论到实践的轻度解析以及如何编辑和微调

nerf模型可编辑的? NeRF模型的可编辑性(editability)指的是能够修改预训练的NeRF模型以改变其生成的场景或对象的某些特征,而不是从头开始重新训练模型。这种编辑可以是改变颜色、形状、纹理或者添加、移除和修改场景中的对象。 在…

在CentOS上设置和管理静态HTTP网站的版本控制

在CentOS上设置和管理静态HTTP网站的版本控制是一项重要的任务,它可以帮助您跟踪和回滚对网站所做的更改,确保数据的一致性和完整性。以下是在CentOS上设置和管理静态HTTP网站的版本控制的步骤: 安装版本控制系统在CentOS上安装Git或其他版本…

LeetCode讲解篇之78. 子集

文章目录 题目描述题解思路题解代码 题目描述 题解思路 初始化一个start变量记录当前从哪里开始遍历搜索nums 搜索过程的数字组合加入结果集 然后从start下标开始遍历nums,更新start,递归搜索 直到搜索完毕,返回结果集 题解代码 class …

记录用python封装的第一个小程序

前言 我要封装的是前段时间复现的一个视频融合拼接的程序,现在我打算将他封装成exe程序,我在这里只记录一下我封装的过程,使用的是pyinstaller,具体的封装知识我就不多说了,可以参考我另一篇博客:将Python…

逼格满满,推荐一个高效测试用例工具:XMind2TestCase !

一、背景 软件测试的核心是什么?毫无疑问是测试分析和测试用例设计,也是日常测试投入最多时间的工作内容之一。 然而,传统的测试用例设计过程有很多痛点: 1、使用Excel表格进行测试用例设计,虽然成本低,但…

POI:对Word的基本操作

1 向word中写入文本并设置样式 package com.example;import org.apache.poi.xwpf.usermodel.*;import java.io.File; import java.io.FileOutputStream;/*** Author:xiexu* Date:2024/1/12 23:54*/ public class WriteWord {static String PATH "…

Linux(Ubantu)交叉编译生成windows(32位,64位)可执行程序和库

机缘 机缘巧合下收到了这个小任务. 工作流 先是找了下资料发现过去的都是关于mingw32. 教程的做法: 增加个源 (trusty 是linux发行的版本标识,比如22.04是jammy deb http://us.archive.ubuntu.com/ubuntu trusty main universe更新源 sudo apt update下载mingw32. sudo…

阅读笔记lv.1

阅读笔记 sql中各种 count结论不同存储引擎计算方式区别count() 类型 责任链模式常见场景例子(闯关游戏) sql中各种 count 结论 innodb count(*) ≈ count(1) > count(主键id) > count(普通索引列) > count(未加索引列)myisam 有专门字段记录…

通过shell脚本确定当前平台

shell中的变量OSTYPE存储操作系统的名称,也可以使用uname命令来确认当前所在的平台。 shell中的变量HOSTTYPE存储操作系统的架构。 测试代码如下所示: #! /bin/bashecho "use OSTYPE:" if [[ "$OSTYPE" "linux-gnu&quo…

java大学生宿舍共享厨房系统宿舍自习室宿舍洗衣房系统源码包含技术文档

主要功能:学生可注册登录,预约自己宿舍楼栋的共享厨房和评价,也可以使用该楼栋的洗衣房,查看洗衣机吹风机的使用情况和报修,还可以进入该楼栋自习室打卡和评价。管理员可管理所有的学生和宿管,分配宿舍&…

MYSQL的操作

1.库的操作 1.1创建数据库 语法: CREATE DATABASE [IF NOT EXISTS] db_name [create_specification [, create_specification] ...] create_specification: [DEFAULT] CHARACTER SET charset_name [DEFAULT] COLLATE collation_name 说明: #…

GitHub项目推荐-incubator

项目地址 Github地址:GitHub - apache/incubator-anser 官网:Apache Answer | Free Open-source Q&A Platform 项目简述 这是Apache的一个开源在线论坛,也可以部署成为一个自有的QA知识库。项目主要使用了Go和Typescript来开发&#…

Feature Fusion for Online Mutual KD

paper:Feature Fusion for Online Mutual Knowledge Distillation official implementation:https://github.com/Jangho-Kim/FFL-pytorch 本文的创新点 本文提出了一个名为特征融合学习(Feature Fusion Learning, FFL)的框架&…

设计模式—— 单例设计模式

单例设计模式 什么是单例模式 单例模式是一种对象创建型模式,使用单例模式,可以保证为一个类只生成唯一的实例对象。也就是说,在整个程序空间中,该类只存在一个实例对象。 为什么使用单例模式 在应用系统开发中,我…

Python测试开发,掌握技巧更上一层楼!

学员学习分享: 自动化测试工作稳定之后,一向对技术着迷的我,迫不及待地要进入测开的世界,在java和Python的方向上,我选择了Python。 原因很简单,比较好上手,而且市场上也比较主流。 在跟着课…

【操作系统】在阅读论文:OrcFS: Orchestrated file system for flash storage是需要补充的基础知

在阅读论文:OrcFS: Orchestrated file system for flash storage是需要补充的基础知识 这篇论文是为了解决软件层次之间的信息冗余问题 To minimize the disk traffic, the file system buffers the updates and then flushes them to the disk as a single unit, …

二极管限幅电路理论分析,工作原理+作用

一、限幅是什么意思? 限幅也就是,将电压限制在某个范围内,去除交流信号的一部分但不会对波形的剩余部分造成影响。通常来说,限幅电路主要是由二极管构成,波形的形状取决于电路的配置和设计。二、限幅电路工作原…

流量预测中文文献阅读(郭郭专用)

目录 基于流量预测的超密集网络资源分配策略研究_2023_高雪亮_内蒙古大学(1)内容总结(2)流量预测部分1、数据集2、结果对其中的一个网格的CDR进行预测RMSE和R2近邻数据和周期数据对RMSE的影响 (3)基于流量预…