ui-ts-layout-flex.md 19.7 KB
Newer Older
K
kukixi 已提交
1 2
# 弹性布局

S
sienna1128 已提交
3 4
弹性布局又称Flex布局,是自适应布局中使用最为灵活的布局。弹性布局提供一种更加有效的方式来对容器中的子元素进行排列、对齐和分配空白空间。
开发者可以通过Flex的接口创建容器组件,进而对容器内的其他元素进行弹性布局。
K
kukixi 已提交
5 6 7


## 创建弹性布局
S
sienna1128 已提交
8
 
K
kukixi 已提交
9 10
接口的调用形式如下:

H
HelloCrease 已提交
11
`Flex(options?: { direction?: FlexDirection, wrap?: FlexWrap,  justifyContent?: FlexAlign, alignItems?: ItemAlign, alignContent?: FlexAlign })`
S
sienna1128 已提交
12 13

通过参数direction定义弹性布局的布局方向,justifyContent定义子组件在弹性布局方向上的对齐方式, alignContent定义子组件在与布局方向垂直的方向上的对齐方式,wrap定义内容超过一行时是否换行。
K
kukixi 已提交
14 15 16

## 弹性布局方向

S
sienna1128 已提交
17
弹性布局有两个方向,子组件排列的方向是主轴,与主轴垂直的方向是交叉轴。通过direction参数设置容器主轴的方向,可选值有:
K
kukixi 已提交
18 19

- FlexDirection.Row(默认值):主轴为水平方向,子组件从起始端沿着水平方向开始排布。
H
HelloCrease 已提交
20

H
HelloCrease 已提交
21
  ```ts
K
kukixi 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35
  Flex({ direction: FlexDirection.Row }) {
    Text('1').width('33%').height(50).backgroundColor(0xF5DEB3)
    Text('2').width('33%').height(50).backgroundColor(0xD2B48C)
    Text('3').width('33%').height(50).backgroundColor(0xF5DEB3)
  }
  .height(70)
  .width('90%')
  .padding(10)
  .backgroundColor(0xAFEEEE)
  ```

  ![zh-cn_image_0000001218579606](figures/zh-cn_image_0000001218579606.png)

- FlexDirection.RowReverse:主轴为水平方向,子组件从终点端沿着FlexDirection.Row相反的方向开始排布。
H
HelloCrease 已提交
36

H
HelloCrease 已提交
37
  ```ts
K
kukixi 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51
  Flex({ direction: FlexDirection.RowReverse }) {
    Text('1').width('33%').height(50).backgroundColor(0xF5DEB3)
    Text('2').width('33%').height(50).backgroundColor(0xD2B48C)
    Text('3').width('33%').height(50).backgroundColor(0xF5DEB3)
  }
  .height(70)
  .width('90%')
  .padding(10)
  .backgroundColor(0xAFEEEE)
  ```

  ![zh-cn_image_0000001218739566](figures/zh-cn_image_0000001218739566.png)

- FlexDirection.Column:主轴为垂直方向,子组件从起始端沿着垂直方向开始排布。
H
HelloCrease 已提交
52

H
HelloCrease 已提交
53
  ```ts
K
kukixi 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67
  Flex({ direction: FlexDirection.Column }) {
    Text('1').width('100%').height(50).backgroundColor(0xF5DEB3)
    Text('2').width('100%').height(50).backgroundColor(0xD2B48C)
    Text('3').width('100%').height(50).backgroundColor(0xF5DEB3)
  }
  .height(70)
  .width('90%')
  .padding(10)
  .backgroundColor(0xAFEEEE)
  ```

  ![zh-cn_image_0000001263019457](figures/zh-cn_image_0000001263019457.png)

- FlexDirection.ColumnReverse:主轴为垂直方向,子组件从终点端沿着FlexDirection.Column相反的方向开始排布。
H
HelloCrease 已提交
68

H
HelloCrease 已提交
69
  ```ts
K
kukixi 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
  Flex({ direction: FlexDirection.ColumnReverse }) {
    Text('1').width('100%').height(50).backgroundColor(0xF5DEB3)
    Text('2').width('100%').height(50).backgroundColor(0xD2B48C)
    Text('3').width('100%').height(50).backgroundColor(0xF5DEB3)
  }
  .height(70)
  .width('90%')
  .padding(10)
  .backgroundColor(0xAFEEEE)
  ```

  ![zh-cn_image_0000001263339459](figures/zh-cn_image_0000001263339459.png)


## 弹性布局换行
默认情况下,子组件在Flex容器中都排在一条线(又称"轴线")上。通过wrap参数设置其他换行方式,可选值有:

S
sienna1128 已提交
87
- FlexWrap.NoWrap(默认值): 不换行。如果子元素的宽度总和大于父元素的宽度,则子元素会被压缩宽度。
H
HelloCrease 已提交
88

H
HelloCrease 已提交
89
  ```ts
K
kukixi 已提交
90 91 92 93 94 95 96 97 98 99 100 101
  Flex({ wrap: FlexWrap.NoWrap }) {
    Text('1').width('50%').height(50).backgroundColor(0xF5DEB3)
    Text('2').width('50%').height(50).backgroundColor(0xD2B48C)
    Text('3').width('50%').height(50).backgroundColor(0xF5DEB3)
  } 
  .width('90%')
  .padding(10)
  .backgroundColor(0xAFEEEE)
  ```

  ![zh-cn_image_0000001263139409](figures/zh-cn_image_0000001263139409.png)

S
sienna1128 已提交
102
- FlexWrap.Wrap:换行,每一行子元素按照主轴方向排列。
H
HelloCrease 已提交
103

H
HelloCrease 已提交
104
  ```ts
K
kukixi 已提交
105 106 107 108 109 110 111 112 113 114 115 116
  Flex({ wrap: FlexWrap.Wrap }) {
    Text('1').width('50%').height(50).backgroundColor(0xF5DEB3)
    Text('2').width('50%').height(50).backgroundColor(0xD2B48C)
    Text('3').width('50%').height(50).backgroundColor(0xD2B48C)
  } 
  .width('90%')
  .padding(10)
  .backgroundColor(0xAFEEEE)
  ```

  ![zh-cn_image_0000001218419614](figures/zh-cn_image_0000001218419614.png)

S
sienna1128 已提交
117
- FlexWrap.WrapReverse:换行,每一行子元素按照主轴反方向排列。
H
HelloCrease 已提交
118

H
HelloCrease 已提交
119
  ```ts
K
kukixi 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
  Flex({ wrap: FlexWrap.WrapReverse}) {
    Text('1').width('50%').height(50).backgroundColor(0xF5DEB3)
    Text('2').width('50%').height(50).backgroundColor(0xD2B48C)
    Text('3').width('50%').height(50).backgroundColor(0xF5DEB3)
  }
  .width('90%')
  .padding(10)
  .backgroundColor(0xAFEEEE)
  ```

  ![zh-cn_image_0000001263259399](figures/zh-cn_image_0000001263259399.png)


## 弹性布局对齐方式

### 主轴对齐

可以通过justifyContent参数设置在主轴的对齐方式,可选值有:

S
sienna1128 已提交
139
- FlexAlign.Start(默认值): 子元素在主轴方向首端对齐, 第一个子元素与父元素边沿对齐,其他元素与前一个元素对齐。
H
HelloCrease 已提交
140

H
HelloCrease 已提交
141
  ```ts
K
kukixi 已提交
142 143
  Flex({ justifyContent: FlexAlign.Start }) {  
    Text('1').width('20%').height(50).backgroundColor(0xF5DEB3)  
S
sienna1128 已提交
144
     Text('3').width('20%').height(50).backgroundColor(0xF5DEB3)
K
kukixi 已提交
145 146
  }
  .width('90%')
S
sienna1128 已提交
147
  .padding({ top: 10, bottom: 10 })
K
kukixi 已提交
148 149 150
  .backgroundColor(0xAFEEEE)
  ```

S
sienna1128 已提交
151
  ![zh-cn_image_0000001218259634](figures/mainStart.png)
K
kukixi 已提交
152

S
sienna1128 已提交
153
- FlexAlign.Center: 子元素在主轴方向居中对齐。
H
HelloCrease 已提交
154

H
HelloCrease 已提交
155
  ```ts
K
kukixi 已提交
156 157 158 159 160 161
  Flex({ justifyContent: FlexAlign.Center }) {  
    Text('1').width('20%').height(50).backgroundColor(0xF5DEB3)  
    Text('2').width('20%').height(50).backgroundColor(0xD2B48C)   
    Text('3').width('20%').height(50).backgroundColor(0xF5DEB3)
  }
  .width('90%')
S
sienna1128 已提交
162
  .padding({ top: 10, bottom: 10 })
K
kukixi 已提交
163 164 165
  .backgroundColor(0xAFEEEE)
  ```

S
sienna1128 已提交
166
  ![zh-cn_image_0000001218579608](figures/mainCenter.png)
K
kukixi 已提交
167

S
sienna1128 已提交
168
- FlexAlign.End: 子元素在主轴方向尾部对齐, 最后一个子元素与父元素边沿对齐,其他元素与后一个元素对齐。
H
HelloCrease 已提交
169

H
HelloCrease 已提交
170
  ```ts
K
kukixi 已提交
171 172 173 174 175 176
  Flex({ justifyContent: FlexAlign.End }) {  
    Text('1').width('20%').height(50).backgroundColor(0xF5DEB3)  
    Text('2').width('20%').height(50).backgroundColor(0xD2B48C)   
    Text('3').width('20%').height(50).backgroundColor(0xF5DEB3)
  }
  .width('90%')
S
sienna1128 已提交
177
  .padding({ top: 10, bottom: 10 })
K
kukixi 已提交
178 179 180
  .backgroundColor(0xAFEEEE)
  ```

S
sienna1128 已提交
181
  ![zh-cn_image_0000001218739568](figures/mainEnd.png)
K
kukixi 已提交
182

S
sienna1128 已提交
183
- FlexAlign.SpaceBetween:  Flex主轴方向均匀分配弹性元素,相邻子元素之间距离相同。第一个子元素和最后一个子元素与父元素边沿对齐。
H
HelloCrease 已提交
184

H
HelloCrease 已提交
185
  ```ts
K
kukixi 已提交
186 187 188 189 190 191
  Flex({ justifyContent: FlexAlign.SpaceBetween }) {  
    Text('1').width('20%').height(50).backgroundColor(0xF5DEB3)  
    Text('2').width('20%').height(50).backgroundColor(0xD2B48C)   
    Text('3').width('20%').height(50).backgroundColor(0xF5DEB3)
  }
  .width('90%')
S
sienna1128 已提交
192
  .padding({ top: 10, bottom: 10 })
K
kukixi 已提交
193 194 195
  .backgroundColor(0xAFEEEE)
  ```

S
sienna1128 已提交
196
  ![zh-cn_image_0000001263019461](figures/mainSpacebetween.png)
K
kukixi 已提交
197

S
sienna1128 已提交
198
- FlexAlign.SpaceAround:  Flex主轴方向均匀分配弹性元素,相邻子元素之间距离相同。 第一个子元素到行首的距离和最后一个子元素到行尾的距离是相邻元素之间距离的一半。
H
HelloCrease 已提交
199

H
HelloCrease 已提交
200
  ```ts
K
kukixi 已提交
201 202 203 204 205 206
  Flex({ justifyContent: FlexAlign.SpaceAround }) {  
    Text('1').width('20%').height(50).backgroundColor(0xF5DEB3)  
    Text('2').width('20%').height(50).backgroundColor(0xD2B48C)   
    Text('3').width('20%').height(50).backgroundColor(0xF5DEB3)
  }
  .width('90%')
S
sienna1128 已提交
207
  .padding({ top: 10, bottom: 10 })
K
kukixi 已提交
208 209 210
  .backgroundColor(0xAFEEEE)
  ```

S
sienna1128 已提交
211
  ![zh-cn_image_0000001263339461](figures/mainSpacearound.png)
K
kukixi 已提交
212

S
sienna1128 已提交
213
- FlexAlign.SpaceEvenly:  Flex主轴方向元素等间距布局,相邻子元素之间的间距、第一个子元素与行首的间距、最后一个子元素到行尾的间距均相等。
H
HelloCrease 已提交
214

H
HelloCrease 已提交
215
  ```ts
K
kukixi 已提交
216 217 218 219 220 221
  Flex({ justifyContent: FlexAlign.SpaceEvenly }) {  
    Text('1').width('20%').height(50).backgroundColor(0xF5DEB3)  
    Text('2').width('20%').height(50).backgroundColor(0xD2B48C)   
    Text('3').width('20%').height(50).backgroundColor(0xF5DEB3)
  }
  .width('90%')
S
sienna1128 已提交
222
  .padding({ top: 10, bottom: 10 })
K
kukixi 已提交
223 224 225
  .backgroundColor(0xAFEEEE)
  ```

S
sienna1128 已提交
226
  ![zh-cn_image_0000001263139411](figures/mainSpaceevenly.png)
K
kukixi 已提交
227 228 229


### 交叉轴对齐
S
sienna1128 已提交
230 231
#### 容器组件设置交叉轴对齐
可以通过flex组件的alignItems参数设置子元素在交叉轴的对齐方式,可选值有:
K
kukixi 已提交
232 233

- ItemAlign.Auto: 使用Flex容器中默认配置。
H
HelloCrease 已提交
234

H
HelloCrease 已提交
235
  ```ts
K
kukixi 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248
  Flex({ alignItems: ItemAlign.Auto }) {  
    Text('1').width('33%').height(30).backgroundColor(0xF5DEB3)  
    Text('2').width('33%').height(40).backgroundColor(0xD2B48C)  
    Text('3').width('33%').height(50).backgroundColor(0xF5DEB3)
  }
  .size({width: '90%', height: 80})
  .padding(10)
  .backgroundColor(0xAFEEEE)
  ```

  ![zh-cn_image_0000001218419616](figures/zh-cn_image_0000001218419616.png)

- ItemAlign.Start: 交叉轴方向首部对齐。
H
HelloCrease 已提交
249

H
HelloCrease 已提交
250
  ```ts
K
kukixi 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263
  Flex({ alignItems: ItemAlign.Start }) {  
    Text('1').width('33%').height(30).backgroundColor(0xF5DEB3)  
    Text('2').width('33%').height(40).backgroundColor(0xD2B48C)  
    Text('3').width('33%').height(50).backgroundColor(0xF5DEB3)
  }
  .size({width: '90%', height: 80})
  .padding(10)
  .backgroundColor(0xAFEEEE)
  ```

  ![zh-cn_image_0000001263259401](figures/zh-cn_image_0000001263259401.png)

- ItemAlign.Center: 交叉轴方向居中对齐。
H
HelloCrease 已提交
264

H
HelloCrease 已提交
265
  ```ts
K
kukixi 已提交
266 267 268 269 270 271 272 273 274 275 276 277 278
  Flex({ alignItems: ItemAlign.Center }) {  
    Text('1').width('33%').height(30).backgroundColor(0xF5DEB3)  
    Text('2').width('33%').height(40).backgroundColor(0xD2B48C)  
    Text('3').width('33%').height(50).backgroundColor(0xF5DEB3)
  }
  .size({width: '90%', height: 80})
  .padding(10)
  .backgroundColor(0xAFEEEE)
  ```

  ![zh-cn_image_0000001218259636](figures/zh-cn_image_0000001218259636.png)

- ItemAlign.End:交叉轴方向底部对齐。
H
HelloCrease 已提交
279

H
HelloCrease 已提交
280
  ```ts
K
kukixi 已提交
281 282 283 284 285 286 287 288 289 290 291 292
  Flex({ alignItems: ItemAlign.End }) {  
    Text('1').width('33%').height(30).backgroundColor(0xF5DEB3)  
    Text('2').width('33%').height(40).backgroundColor(0xD2B48C)  
    Text('3').width('33%').height(50).backgroundColor(0xF5DEB3)
  }
  .size({width: '90%', height: 80})
  .padding(10)
  .backgroundColor(0xAFEEEE)
  ```

  ![zh-cn_image_0000001218579610](figures/zh-cn_image_0000001218579610.png)

293
- ItemAlign.Stretch:交叉轴方向拉伸填充,在未设置尺寸时,拉伸到容器尺寸。
H
HelloCrease 已提交
294

H
HelloCrease 已提交
295
  ```ts
K
kukixi 已提交
296 297 298 299 300 301 302 303 304 305 306 307 308
  Flex({ alignItems: ItemAlign.Stretch }) {  
    Text('1').width('33%').height(30).backgroundColor(0xF5DEB3)  
    Text('2').width('33%').height(40).backgroundColor(0xD2B48C)  
    Text('3').width('33%').height(50).backgroundColor(0xF5DEB3)
  }
  .size({width: '90%', height: 80})
  .padding(10)
  .backgroundColor(0xAFEEEE)
  ```

  ![zh-cn_image_0000001218739570](figures/zh-cn_image_0000001218739570.png)

- ItemAlign.Baseline:交叉轴方向文本基线对齐。
H
HelloCrease 已提交
309

H
HelloCrease 已提交
310
  ```ts
K
kukixi 已提交
311 312 313 314 315 316 317 318 319 320 321 322 323
  Flex({ alignItems: ItemAlign.Baseline }) {  
    Text('1').width('33%').height(30).backgroundColor(0xF5DEB3)  
    Text('2').width('33%').height(40).backgroundColor(0xD2B48C)  
    Text('3').width('33%').height(50).backgroundColor(0xF5DEB3)
  }
  .size({width: '90%', height: 80})
  .padding(10)
  .backgroundColor(0xAFEEEE)
  ```

  ![zh-cn_image_0000001263019463](figures/zh-cn_image_0000001263019463.png)


S
sienna1128 已提交
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
#### 子组件设置交叉轴对齐

子组件的alignSelf属性也可以设置子组件在父容器交叉轴的对齐格式,且会覆盖Flex布局容器中alignItems默认配置。如下例所示:

```ts
Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center }) {
  Text('alignSelf Start').width('25%').height(80)
    .alignSelf(ItemAlign.Start)
    .backgroundColor(0xF5DEB3)
  Text('alignSelf Baseline')
    .alignSelf(ItemAlign.Baseline)
    .width('25%')
    .height(80)
    .backgroundColor(0xD2B48C)
  Text('alignSelf Baseline').width('25%').height(100)
    .backgroundColor(0xF5DEB3)
    .alignSelf(ItemAlign.Baseline)
  Text('no alignSelf').width('25%').height(100)
    .backgroundColor(0xD2B48C)
  Text('no alignSelf').width('25%').height(100)
    .backgroundColor(0xF5DEB3)

}.width('90%').height(220).backgroundColor(0xAFEEEE)
```

![](figures/alignself.png)

上例中,Flex容器中alignItems设置交叉轴子组件的对齐方式为居中,子组件自身设置了alignSelf属性的情况,覆盖父组件的alignItem值,表现为alignSelf的定义。

K
kukixi 已提交
353 354
### 内容对齐

S
sienna1128 已提交
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
可以通过alignContent参数设置子元素各行在交叉轴剩余空间内的对齐方式,只在多行的flex布局中生效,可选值有:

- FlexAlign.Start: 子元素各行与交叉轴起点对齐。

  ``` 
  Flex({ justifyContent: FlexAlign.SpaceBetween, wrap: FlexWrap.Wrap, alignContent: FlexAlign.Start }) {
    Text('1').width('30%').height(20).backgroundColor(0xF5DEB3)
    Text('2').width('60%').height(20).backgroundColor(0xD2B48C)
    Text('3').width('40%').height(20).backgroundColor(0xD2B48C)
    Text('4').width('30%').height(20).backgroundColor(0xF5DEB3)
    Text('5').width('20%').height(20).backgroundColor(0xD2B48C)
  }
  .width('90%')
  .height(100)
  .backgroundColor(0xAFEEEE)          
  ```
  
  ![crossStart.png](figures/crossStart.png)

- FlexAlign.Center: 子元素各行在交叉轴方向居中对齐。

  ```ts
  Flex({ justifyContent: FlexAlign.SpaceBetween, wrap: FlexWrap.Wrap, alignContent: FlexAlign.Center }) {
    Text('1').width('30%').height(20).backgroundColor(0xF5DEB3)
    Text('2').width('60%').height(20).backgroundColor(0xD2B48C)
    Text('3').width('40%').height(20).backgroundColor(0xD2B48C)
    Text('4').width('30%').height(20).backgroundColor(0xF5DEB3)
    Text('5').width('20%').height(20).backgroundColor(0xD2B48C)
  }
  .width('90%')
  .height(100)
  .backgroundColor(0xAFEEEE)          
  ```
  ![crossCenter.png](figures/crossCenter.png)
- FlexAlign.End: 子元素各行与交叉轴终点对齐。

  ```ts
  Flex({ justifyContent: FlexAlign.SpaceBetween, wrap: FlexWrap.Wrap, alignContent: FlexAlign.End }) {
    Text('1').width('30%').height(20).backgroundColor(0xF5DEB3)
    Text('2').width('60%').height(20).backgroundColor(0xD2B48C)
    Text('3').width('40%').height(20).backgroundColor(0xD2B48C)
    Text('4').width('30%').height(20).backgroundColor(0xF5DEB3)
    Text('5').width('20%').height(20).backgroundColor(0xD2B48C)
  }
  .width('90%')
  .height(100)
  .backgroundColor(0xAFEEEE)          
  ```
  ![crossEnd.png](figures/crossEnd.png)
- FlexAlign.SpaceBetween: 子元素各行与交叉轴两端对齐,各行间垂直间距平均分布。

  ```ts
  Flex({ justifyContent: FlexAlign.SpaceBetween, wrap: FlexWrap.Wrap, alignContent: FlexAlign.SpaceBetween }) {
    Text('1').width('30%').height(20).backgroundColor(0xF5DEB3)
    Text('2').width('60%').height(20).backgroundColor(0xD2B48C)
    Text('3').width('40%').height(20).backgroundColor(0xD2B48C)
    Text('4').width('30%').height(20).backgroundColor(0xF5DEB3)
    Text('5').width('20%').height(20).backgroundColor(0xD2B48C)
  }
  .width('90%')
  .height(100)
  .backgroundColor(0xAFEEEE)          
  ```
  ![crossSpacebetween.png](figures/crossSpacebetween.png)

- FlexAlign.SpaceAround: 子元素各行间距相等,是元素首尾行与交叉轴两端距离的两倍。

  ```ts
  Flex({ justifyContent: FlexAlign.SpaceBetween, wrap: FlexWrap.Wrap, alignContent: FlexAlign.SpaceAround }) {
    Text('1').width('30%').height(20).backgroundColor(0xF5DEB3)
    Text('2').width('60%').height(20).backgroundColor(0xD2B48C)
    Text('3').width('40%').height(20).backgroundColor(0xD2B48C)
    Text('4').width('30%').height(20).backgroundColor(0xF5DEB3)
    Text('5').width('20%').height(20).backgroundColor(0xD2B48C)
  }
  .width('90%')
  .height(100)
  .backgroundColor(0xAFEEEE)          
  ```
  
  ![crossSpacearound.png](figures/crossSpacearound.png)

- FlexAlign.SpaceEvenly:  子元素各行间距,子元素首尾行与交叉轴两端距离都相等。
K
kukixi 已提交
438

S
sienna1128 已提交
439 440 441 442 443 444 445 446 447 448 449 450 451 452
  ```ts
  Flex({ justifyContent: FlexAlign.SpaceBetween, wrap: FlexWrap.Wrap, alignContent: FlexAlign.SpaceAround }) {
    Text('1').width('30%').height(20).backgroundColor(0xF5DEB3)
    Text('2').width('60%').height(20).backgroundColor(0xD2B48C)
    Text('3').width('40%').height(20).backgroundColor(0xD2B48C)
    Text('4').width('30%').height(20).backgroundColor(0xF5DEB3)
    Text('5').width('20%').height(20).backgroundColor(0xD2B48C)
  }
  .width('90%')
  .height(100)
  .backgroundColor(0xAFEEEE)          
  ```
  
  ![crossSpaceevenly.png](figures/crossSpaceevenly.png)
K
kukixi 已提交
453 454


S
sienna1128 已提交
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
## 弹性布局的自适应拉伸
在弹性布局父组件尺寸不够大的时候,通过子组件的下面几个属性设置其再父容器的占比,达到自适应布局能力。
1. flexBasis: 设置子组件在父容器主轴方向上的基准尺寸。如果设置了该值,则子项占用的空间为设置的值;如果没设置或者为auto,那子项的空间为width/height的值。
```ts
Flex() {
  Text('flexBasis("auto")')
    .flexBasis('auto') // 未设置width以及flexBasis值为auto,内容自身宽松
    .height(100)
    .backgroundColor(0xF5DEB3)
  Text('flexBasis("auto")'+' width("40%")')
    .width('40%')
    .flexBasis('auto') //设置width以及flexBasis值auto,使用width的值
    .height(100)
    .backgroundColor(0xD2B48C)


  Text('flexBasis(100)')  // 未设置width以及flexBasis值为100,宽度为100vp
    .flexBasis(100)  
    .height(100)
    .backgroundColor(0xF5DEB3)

  Text('flexBasis(100)')
    .flexBasis(100)
    .width(200) // flexBasis值为100,覆盖width的设置值,宽度为100vp
    .height(100)
    .backgroundColor(0xD2B48C)
}.width('90%').height(120).padding(10).backgroundColor(0xAFEEEE)
```
![](figures/flexbasis.png)
K
kukixi 已提交
484

S
sienna1128 已提交
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
2. flexGrow: 设置父容器的剩余空间分配给此属性所在组件的比例。用于"瓜分"父组件的剩余空间。
```ts
Flex() {
  Text('flexGrow(1)')
    .flexGrow(2) 
    .width(100)
    .height(100)
    .backgroundColor(0xF5DEB3)
  
  Text('flexGrow(3)')
    .flexGrow(2)
    .width(100)
    .height(100)
    .backgroundColor(0xD2B48C)

  Text('no flexGrow')
    .width(100) 
    .height(100)
    .backgroundColor(0xF5DEB3)
}.width(400).height(120).padding(10).backgroundColor(0xAFEEEE)
```
K
kukixi 已提交
506

S
sienna1128 已提交
507
![](figures/flexgrow.png)
K
kukixi 已提交
508

S
sienna1128 已提交
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
上图中,父容器宽度400vp,三个子元素原始宽度为100vp,综合300vp,剩余空间100vp根据flexGrow值的占比分配给子元素,未设置flexGrow的子元素不参与“瓜分”。
第一个元素以及第二个元素以2:3分配剩下的100vp。第一个元素为100vp+100vp*2/5=140vp,第二个元素为100vp+100vp*3/5=160vp。

3. flexShrink: 当父容器空间不足时,子元素的压缩比例。
```ts
Flex({ direction: FlexDirection.Row }) {
  Text('flexShrink(3)')
    .flexShrink(3)
    .width(200)
    .height(100)
    .backgroundColor(0xF5DEB3)
  
  Text('no flexShrink')
    .width(200)
    .height(100)
    .backgroundColor(0xD2B48C)

  Text('flexShrink(2)')
    .flexShrink(2)
    .width(200)
    .height(100)
    .backgroundColor(0xF5DEB3)  
}.width(400).height(120).padding(10).backgroundColor(0xAFEEEE) 
```
![](figures/flexshrink.png)
K
kukixi 已提交
534 535 536 537


## 场景示例

S
sienna1128 已提交
538
使用弹性布局,可以实现子元素沿水平方向排列,两端对齐,子元素间距平分,竖直方向上子元素居中的效果。示例如下:
K
kukixi 已提交
539

H
HelloCrease 已提交
540
```ts
S
sienna1128 已提交
541
@Entry  
K
kukixi 已提交
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
@Component
struct FlexExample {
  build() {
    Column() {
      Column({ space: 5 }) {
        Flex({ direction: FlexDirection.Row, wrap: FlexWrap.NoWrap, justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign.Center }) {
          Text('1').width('30%').height(50).backgroundColor(0xF5DEB3)
          Text('2').width('30%').height(50).backgroundColor(0xD2B48C)
          Text('3').width('30%').height(50).backgroundColor(0xF5DEB3)
        }
        .height(70)
        .width('90%')
        .backgroundColor(0xAFEEEE)
      }.width('100%').margin({ top: 5 })
    }.width('100%') 
 }
}
```


S
sienna1128 已提交
562
![zh-cn_image_0000001261605867](figures/flexExample.png)
563 564 565 566 567 568


## 相关实例

针对弹性布局开发,有以下相关实例可供参考:

569
- [弹性布局(eTS)(API8)](https://gitee.com/openharmony/codelabs/tree/master/ETSUI/FlowLayoutEts)
570 571

- [ArkUI常用布局容器对齐方式(eTS)(API9)](https://gitee.com/openharmony/codelabs/tree/master/ETSUI/LayoutAlignmentDemo)