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