• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 层叠布局 (Stack)
2
3
4## 概述
5
6层叠布局(StackLayout)用于在屏幕上预留一块区域来显示组件中的元素,提供元素可以重叠的布局。层叠布局通过[Stack](../reference/apis-arkui/arkui-ts/ts-container-stack.md)容器组件实现位置的固定定位与层叠,容器中的子元素依次入栈,后一个子元素覆盖前一个子元素,子元素可以叠加,也可以设置位置。
7
8层叠布局具有较强的页面层叠、位置定位能力,其使用场景有广告、卡片层叠效果等。
9
10如图1,Stack作为容器,容器内的子元素的顺序为Item1->Item2->Item3。
11
12
13  **图1** 层叠布局  
14
15![stack-layout](figures/stack-layout.png)
16
17
18## 开发布局
19
20Stack组件为容器组件,容器内可包含各种子元素。其中子元素默认进行居中堆叠。子元素被约束在Stack下,进行自己的样式定义以及排列。
21
22
23
24```ts
25let MTop:Record<string,number> = { 'top': 50 }
26Column(){
27  Stack({ }) {
28    Column(){}.width('90%').height('100%').backgroundColor('#ff58b87c')
29    Text('text').width('60%').height('60%').backgroundColor('#ffc3f6aa')
30    Button('button').width('30%').height('30%').backgroundColor('#ff8ff3eb').fontColor('#000')
31  }.width('100%').height(150).margin(MTop)
32}
33```
34
35
36![stack-layout-sample](figures/stack-layout-sample.png)
37
38
39## 对齐方式
40
41Stack组件通过[alignContent参数](../reference/apis-arkui/arkui-ts/ts-appendix-enums.md#alignment)实现位置的相对移动。如图2所示,支持九种对齐方式。
42
43  **图2** Stack容器内元素的对齐方式  
44
45![zh-cn_image_0000001562940621](figures/zh-cn_image_0000001562940621.png)
46
47```ts
48// xxx.ets
49@Entry
50@Component
51struct StackExample {
52  build() {
53    Stack({ alignContent: Alignment.TopStart }) {
54      Text('Stack').width('90%').height('100%').backgroundColor('#e1dede').align(Alignment.BottomEnd)
55      Text('Item 1').width('70%').height('80%').backgroundColor(0xd2cab3).align(Alignment.BottomEnd)
56      Text('Item 2').width('50%').height('60%').backgroundColor(0xc1cbac).align(Alignment.BottomEnd)
57    }.width('100%').height(150).margin({ top: 5 })
58  }
59}
60```
61
62## Z序控制
63
64Stack容器中兄弟组件显示层级关系可以通过[Z序控制](../reference/apis-arkui/arkui-ts/ts-universal-attributes-z-order.md)的zIndex属性改变。zIndex值越大,显示层级越高,即zIndex值大的组件会覆盖在zIndex值小的组件上方。
65
66  在层叠布局中,如果后面子元素尺寸大于前面子元素尺寸,则前面子元素完全隐藏。
67
68```ts
69let MTopM1:Record<string,number> = { 'top': 100 }
70Stack({ alignContent: Alignment.BottomStart }) {
71  Column() {
72    Text('Stack子元素1').textAlign(TextAlign.End).fontSize(20)
73  }.width(100).height(100).backgroundColor(0xffd306)
74
75  Column() {
76    Text('Stack子元素2').fontSize(20)
77  }.width(150).height(150).backgroundColor(Color.Pink)
78
79  Column() {
80    Text('Stack子元素3').fontSize(20)
81  }.width(200).height(200).backgroundColor(Color.Grey)
82}.margin(MTopM1).width(350).height(350).backgroundColor(0xe0e0e0)
83```
84
85![zh-cn_image_0000001511900544](figures/zh-cn_image_0000001511900544.png)
86
87上图中,最后的子元素3的尺寸大于前面的所有子元素,所以,前面两个元素完全隐藏。改变子元素1,子元素2的zIndex属性后,可以将元素展示出来。
88
89
90```ts
91let MTopM:Record<string,number> = { 'top': 100 }
92Stack({ alignContent: Alignment.BottomStart }) {
93  Column() {
94    Text('Stack子元素1').fontSize(20)
95  }.width(100).height(100).backgroundColor(0xffd306).zIndex(2)
96
97  Column() {
98    Text('Stack子元素2').fontSize(20)
99  }.width(150).height(150).backgroundColor(Color.Pink).zIndex(1)
100
101  Column() {
102    Text('Stack子元素3').fontSize(20)
103  }.width(200).height(200).backgroundColor(Color.Grey)
104}.margin(MTopM).width(350).height(350).backgroundColor(0xe0e0e0)
105```
106
107![zh-cn_image_0000001563060797](figures/zh-cn_image_0000001563060797.png)
108
109
110## 场景示例
111
112使用层叠布局快速搭建页面。
113
114
115```ts
116@Entry
117@Component
118struct StackSample {
119  private arr: string[] = ['APP1', 'APP2', 'APP3', 'APP4', 'APP5', 'APP6', 'APP7', 'APP8'];
120
121  build() {
122    Stack({ alignContent: Alignment.Bottom }) {
123      Flex({ wrap: FlexWrap.Wrap }) {
124        ForEach(this.arr, (item:string) => {
125          Text(item)
126            .width(100)
127            .height(100)
128            .fontSize(16)
129            .margin(10)
130            .textAlign(TextAlign.Center)
131            .borderRadius(10)
132            .backgroundColor(0xFFFFFF)
133        }, (item:string):string => item)
134      }.width('100%').height('100%')
135
136      Flex({ justifyContent: FlexAlign.SpaceAround, alignItems: ItemAlign.Center }) {
137        Text('联系人').fontSize(16)
138        Text('设置').fontSize(16)
139        Text('短信').fontSize(16)
140      }
141      .width('50%')
142      .height(50)
143      .backgroundColor('#16302e2e')
144      .margin({ bottom: 15 })
145      .borderRadius(15)
146    }.width('100%').height('100%').backgroundColor('#CFD0CF')
147  }
148}
149```
150
151
152![zh-cn_image_0000001511421368](figures/zh-cn_image_0000001511421368.png)
153