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