层叠布局(Stack)

目录

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.mfbz.cn/a/318285.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

ChatGPT能帮助我们人类做什么

一、ChatGPT可以在多个方面帮助人类: 回答问题: ChatGPT可以回答各种问题,提供信息和解释概念。 创造性写作: 它可以生成文章、故事、诗歌等创意性文本。 学术辅助: ChatGPT可以辅助学术研究,提供解释、背…

如何生成文本: 通过 Transformers 用不同的解码方法生成文本

如何生成文本: 通过 Transformers 用不同的解码方法生成文本 假设 $p0.92$,Top-p 采样对单词概率进行降序排列并累加,然后选择概率和首次超过 $p92%$ 的单词集作为采样池,定义为 $V_{\text{top-p}}$。在 $t1$ 时 $V_{\text{top-p}}$ 有 9 个…

串行Nor Flash的结构和参数特性

文章目录 引言1、Nor Flash的结构2、Nor Flash的类别3.标准Serial Nor Flash的特征属性1.Wide Range VCC Flash2.Permanent Lock3.Default Lock Protection4.Standard Serial Interface5.Multi-I/O6.Multi-I/O Duplex (DTR)7.XIP(片上执行) 4.标准Serial…

Java SE入门及基础(11)

程序调试 1. 什么是程序调试 当程序出现问题时,我们希望程序能够暂停下来,然后通过我们操作使代码逐行执行,观察整个过程中变量的变化是否按照我们设计程序的思维变化,从而找问题并解决问题,这个过程称之为程序调试…

Openstack组件glance对接swift

2、glance对接swift (1)可直接在数据库中查看镜像存放的位置、状态、id等信息 (2)修改glance-api的配置文件,实现对接swift存储(配置文件在/etc/glance/glance-api.conf,建议先拷贝一份&#x…

基于反卷积方法的重大突破:结构光系统中的测量误差降低3倍

作者:小柠檬 | 来源:3DCV 在公众号「3DCV」后台,回复「原论文」可获取论文pdf 结构光三维测量技术在工业自动化、逆向工程和图形学领域越来越受欢迎。然而,现有的测量系统在成像过程中存在不完美,会导致在不连续边缘周…

nuxt pm2使用、启动、问题解决方案

pm2简介 pm2是一个进程管理工具,可以用它来管理node进程,并查看node进程的状态,当然也支持性能监控,进程守护,负载均衡等功能,在前端和nodejs的世界中用的很多 pm2安装 安装pm2: $ npm install -g pm2查看pm2的安装…

Kafka基本介绍

消息队列 产生背景 消息队列:指的数据在一个容器中,从容器中一端传递到另一端的过程 消息(message): 指的是数据,只不过这个数据存在一定流动状态 队列(queue): 指的容器,可以存储数据,只不过这个容器具备FIFO(先进…

Linux技术,winSCP连接服务器超时故障解决方案

知识改变命运,技术就是要分享,有问题随时联系,免费答疑,欢迎联系! 故障现象 使用 sftp 协议连接主机时, 明显感觉缓慢且卡顿,并且时常出现如下报错: 点击重新连接后,又有概率重新连接上; 总之在"连接上"和&…

蓝桥杯练习题(六)

📑前言 本文主要是【算法】——蓝桥杯练习题(六)的文章,如果有什么需要改进的地方还请大佬指出⛺️ 🎬作者简介:大家好,我是听风与他🥇 ☁️博客首页:CSDN主页听风与他 …

分布式系统的三字真经CAP

文章目录 前言C(Consistency 数据一致性)A(Availability 服务可用性)P(Partition Tolerance 分区容错性)CAP理论最后 前言 你好,我是醉墨居士,我一起探索一下分布式系统的三字真经C…

Linux完全卸载Anaconda3和MiniConda3

如何安装Anaconda3和MiniConda3请看这篇文章: 安装Anaconda3和MiniConda3_minianaconda3-CSDN博客文章浏览阅读474次。MiniConda3官方版是一款优秀的Python环境管理软件。MiniConda3最新版只包含conda及其依赖项如果您更愿意拥有conda以及超过720个开源软件包&…

怎么安装es、kibana(单点安装)

1.部署单点es 1.1.创建网络 因为我们还需要部署kibana容器,因此需要让es和kibana容器互联。这里先创建一个网络: docker network create es-net1.2.加载镜像 这里我们采用elasticsearch的7.12.1版本的镜像,这个镜像体积非常大&#xff0c…

增删改查管理系统 总结1

//提醒: 管理员也要有增删改查 新增员工代码完善2可能需要用到 目录 细节1 pom文件出现奇怪页面? 细节2 如何联系DataGrip与idea? 细节3 Yapi?接口文档?如何有以下画面? ​细节4 如何将时间转化为好看的时间&…

PostgreSQL认证考试PGCA、PGCE、PGCM

PostgreSQL认证考试PGCA、PGCE、PGCM 【重点!重点!重点!】PGCA、PGCE、PGCM 直通车快速下正,省心省力,每2个月一次考试 PGCE考试通知 (2024) 一、考试概览 (一) 报名要…

必练的100道C语言程序设计练习题(上)

前言: 在计算机编程的世界中,C语言一直是一门备受推崇的语言。它的简洁性、高效性以及广泛应用使得学习C语言成为每一位程序员的必由之路。然而,掌握这门语言并不是一蹴而就的事情,它需要不断的练习和实践。为了帮助各位编程爱好者更好地理解…

kafka之java客户端实战

1. kafka的客户端 Kafka提供了两套客户端API,HighLevel API和LowLevel API。 HighLevel API封装了kafka的运行细节,使用起来比较简单,是企业开发过程中最常用的客户端API。 而LowLevel API则需要客户端自己管理Kafka的运行细节,Pa…

通过shell脚本确定当前平台

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

canvas设置渐变色文字(线性、径向)

查看专栏目录 canvas示例教程100专栏,提供canvas的基础知识,高级动画,相关应用扩展等信息。canvas作为html的一部分,是图像图标地图可视化的一个重要的基础,学好了canvas,在其他的一些应用上将会起到非常重…

ssm基于JAVA的酒店客房管理系统论文

摘 要 现代经济快节奏发展以及不断完善升级的信息化技术,让传统数据信息的管理升级为软件存储,归纳,集中处理数据信息的管理方式。本酒店客房管理系统就是在这样的大环境下诞生,其可以帮助管理者在短时间内处理完毕庞大的数据信息…
最新文章