• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Stack Layout
2
3
4## Overview
5
6The stack layout reserves an area on the screen to display elements in a component and allows the elements to be stacked. You can implement a stack layout through the [\<Stack>](../reference/arkui-ts/ts-container-stack.md) component, which provides a stack container where positioned or non-positioned child components are pushed successively and the latter one sits on top of the previous one.
7
8The stack layout excels at page stacking and positioning, and is widely used in ads and widget arrangement.
9
10In the **\<Stack>** component shown in Figure 1, the sequence of child elements (child components) is Item1 -> Item2 -> Item3.
11
12
13  **Figure 1** Stack layout
14
15![stack-layout](figures/stack-layout.png)
16
17
18## How to Develop
19
20The **\<Stack>** component can contain a wide variety of child components, which are stacked in the center by default based on their sizes. While respecting the constraints of **\<Stack>**, child components are laid out in their respective style.
21
22
23
24```ts
25Column(){
26  Stack({ }) {
27    Column(){}.width('90%').height('100%').backgroundColor('#ff58b87c')
28    Text('text').width('60%').height('60%').backgroundColor('#ffc3f6aa')
29    Button('button').width('30%').height('30%').backgroundColor('#ff8ff3eb').fontColor('#000')
30  }.width('100%').height(150).margin({ top: 50 })
31}
32```
33
34
35![stack-layout-sample](figures/stack-layout-sample.png)
36
37
38## Alignment
39
40Alignment of child components in the **\<Stack>** component is set through the [alignContent](../reference/arkui-ts/ts-appendix-enums.md#alignment) parameter. As shown in Figure 2, nine alignment modes are supported.
41
42  **Figure 2** Alignment modes in the \<Stack> component
43
44![en-us_image_0000001562940621](figures/en-us_image_0000001562940621.png)
45
46
47## Z-order Control
48
49The stacking order of child components in the **\<Stack>** component is set through the **[zIndex](../reference/arkui-ts/ts-universal-attributes-z-order.md)** attribute. A larger **zIndex** value indicates a higher display level.
50
51  In the stack layout, if the size of a component is greater than that of the one before it, the one before it is hidden.
52
53```ts
54Stack({ alignContent: Alignment.BottomStart }) {
55  Column() {
56    Text ('Stacked component 1').textAlign (TextAlign.End).fontSize (20)
57  }.width(100).height(100).backgroundColor(0xffd306)
58
59  Column() {
60    Text ('Stacked component 2').fontSize (20)
61  }.width(150).height(150).backgroundColor(Color.Pink)
62
63  Column() {
64    Text ('Stacked component 3').fontSize (20)
65  }.width(200).height(200).backgroundColor(Color.Grey)
66}.margin({ top: 100 }).width(350).height(350).backgroundColor(0xe0e0e0)
67```
68
69![en-us_image_0000001511900544](figures/en-us_image_0000001511900544.png)
70
71In the following figure, the size of the stacked component 3 is greater than that of all the components before it. Therefore, the first two components are completely hidden. To show these components, modify their **zIndex** attribute settings.
72
73
74```ts
75Stack({ alignContent: Alignment.BottomStart }) {
76  Column() {
77    Text ('Stacked component 1').fontSize (20)
78  }.width(100).height(100).backgroundColor(0xffd306).zIndex(2)
79
80  Column() {
81    Text ('Stacked component 2').fontSize (20)
82  }.width(150).height(150).backgroundColor(Color.Pink).zIndex(1)
83
84  Column() {
85    Text ('Stacked component 3').fontSize (20)
86  }.width(200).height(200).backgroundColor(Color.Grey)
87}.margin({ top: 100 }).width(350).height(350).backgroundColor(0xe0e0e0)
88```
89
90![en-us_image_0000001563060797](figures/en-us_image_0000001563060797.png)
91
92
93## Example Scenario
94
95In this example, the stack layout is used to quickly set up a page display model.
96
97
98```ts
99@Entry
100@Component
101struct StackSample {
102  private arr: string[] = ['APP1', 'APP2', 'APP3', 'APP4', 'APP5', 'APP6', 'APP7', 'APP8'];
103
104  build() {
105    Stack({ alignContent: Alignment.Bottom }) {
106      Flex({ wrap: FlexWrap.Wrap }) {
107        ForEach(this.arr, (item) => {
108          Text(item)
109            .width(100)
110            .height(100)
111            .fontSize(16)
112            .margin(10)
113            .textAlign(TextAlign.Center)
114            .borderRadius(10)
115            .backgroundColor(0xFFFFFF)
116        }, item => item)
117      }.width('100%').height('100%')
118
119      Flex({ justifyContent: FlexAlign.SpaceAround, alignItems: ItemAlign.Center }) {
120        Text ('Contacts').fontSize (16)
121        Text ('Settings').fontSize (16)
122        Text ('Messaging').fontSize (16)
123      }
124      .width('50%')
125      .height(50)
126      .backgroundColor('#16302e2e')
127      .margin({ bottom: 15 })
128      .borderRadius(15)
129    }.width('100%').height('100%').backgroundColor('#CFD0CF')
130  }
131}
132```
133
134
135![en-us_image_0000001511421368](figures/en-us_image_0000001511421368.png)
136