ui-ts-layout-flex.md 12.7 KB
Newer Older
K
kukixi 已提交
1 2 3 4 5 6 7 8 9 10
# 弹性布局


Flex组件用于创建弹性布局,开发者可以通过Flex的接口创建容器组件,进而对容器内的其他元素进行弹性布局,例如:使三个元素在容器内水平居中,垂直等间隔分散。


## 创建弹性布局

接口的调用形式如下:

H
HelloCrease 已提交
11
`Flex(options?: { direction?: FlexDirection, wrap?: FlexWrap,  justifyContent?: FlexAlign, alignItems?: ItemAlign, alignContent?: FlexAlign })`
K
kukixi 已提交
12 13 14 15 16 17 18
通过参数direction定义弹性布局的布局方向,justifyContent定义弹性布局方向上的子组件对齐方式, alignContent定义与布局方向垂直的方向上的子组件对齐方式,wrap定义内容超过一行时是否换行。

## 弹性布局方向

弹性布局有两个方向,子组件放置的方向是主轴,与主轴垂直的方向是交叉轴。通过direction参数设置容器主轴的方向,可选值有:

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

H
HelloCrease 已提交
20
  ```ts
K
kukixi 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34
  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 已提交
35

H
HelloCrease 已提交
36
  ```ts
K
kukixi 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50
  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 已提交
51

H
HelloCrease 已提交
52
  ```ts
K
kukixi 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66
  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 已提交
67

H
HelloCrease 已提交
68
  ```ts
K
kukixi 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
  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参数设置其他换行方式,可选值有:

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

H
HelloCrease 已提交
89
  ```ts
K
kukixi 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102
  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)

- FlexWrap.Wrap:换行。
H
HelloCrease 已提交
103

H
HelloCrease 已提交
104
  ```ts
K
kukixi 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117
  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)

- 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 139 140
  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参数设置在主轴的对齐方式,可选值有:

- FlexAlign.Start: 元素在主轴方向首端对齐, 第一个元素与行首对齐,同时后续的元素与前一个对齐。
H
HelloCrease 已提交
141

H
HelloCrease 已提交
142
  ```ts
K
kukixi 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155
  Flex({ justifyContent: FlexAlign.Start }) {  
    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%')
  .padding(10)
  .backgroundColor(0xAFEEEE)
  ```

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

- FlexAlign.Center: 元素在主轴方向中心对齐,第一个元素与行首的距离与最后一个元素与行尾距离相同。
H
HelloCrease 已提交
156

H
HelloCrease 已提交
157
  ```ts
K
kukixi 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170
  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%')
  .padding(10)
  .backgroundColor(0xAFEEEE)
  ```

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

- FlexAlign.End: 元素在主轴方向尾部对齐,  最后一个元素与行尾对齐,其他元素与后一个对齐。
H
HelloCrease 已提交
171

H
HelloCrease 已提交
172
  ```ts
K
kukixi 已提交
173 174 175 176 177 178 179 180 181 182 183 184
  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%')
  .padding(10)
  .backgroundColor(0xAFEEEE)
  ```

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

185
- FlexAlign.SpaceBetween:  Flex主轴方向均匀分配弹性元素,相邻元素之间距离相同。 第一个元素与行首对齐,最后一个元素与行尾对齐。
H
HelloCrease 已提交
186

H
HelloCrease 已提交
187
  ```ts
K
kukixi 已提交
188 189 190 191 192 193 194 195 196 197 198 199
  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%')
  .padding(10)
  .backgroundColor(0xAFEEEE)
  ```

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

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

H
HelloCrease 已提交
202
  ```ts
K
kukixi 已提交
203 204 205 206 207 208 209 210 211 212 213 214
  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%')
  .padding(10)
  .backgroundColor(0xAFEEEE)
  ```

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

215
- FlexAlign.SpaceEvenly:  Flex主轴方向元素等间距布局, 相邻元素之间的间距、第一个元素与行首的间距、最后一个元素到行尾的间距都完全一样。
H
HelloCrease 已提交
216

H
HelloCrease 已提交
217
  ```ts
K
kukixi 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
  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%')
  .padding(10)
  .backgroundColor(0xAFEEEE)
  ```

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


### 交叉轴对齐

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

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

H
HelloCrease 已提交
237
  ```ts
K
kukixi 已提交
238 239 240 241 242 243 244 245 246 247 248 249 250
  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 已提交
251

H
HelloCrease 已提交
252
  ```ts
K
kukixi 已提交
253 254 255 256 257 258 259 260 261 262 263 264 265
  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 已提交
266

H
HelloCrease 已提交
267
  ```ts
K
kukixi 已提交
268 269 270 271 272 273 274 275 276 277 278 279 280
  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 已提交
281

H
HelloCrease 已提交
282
  ```ts
K
kukixi 已提交
283 284 285 286 287 288 289 290 291 292 293 294
  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)

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

H
HelloCrease 已提交
297
  ```ts
K
kukixi 已提交
298 299 300 301 302 303 304 305 306 307 308 309 310
  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 已提交
311

H
HelloCrease 已提交
312
  ```ts
K
kukixi 已提交
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
  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)


### 内容对齐

可以通过alignContent参数设置在换行组件的行在交叉轴剩余空间内的对齐方式,可选值有:

- FlexAlign.Start: 左对齐。

- FlexAlign.Center: 居中对齐。

- FlexAlign.End: 右对齐。

336
- FlexAlign.SpaceBetween:  flex items之间的距离相等,与main start、main end两端对齐。
K
kukixi 已提交
337

338
- FlexAlign.SpaceAround:  flex items之间的距离相等,flex items与main start、main end之间的距离等于flex items之间距离的一半。
K
kukixi 已提交
339

340
- FlexAlign.SpaceEvenly:  flex items之间的距离相等,flex items与main start、main end之间的距离等于flex items之间的距离。
K
kukixi 已提交
341 342 343 344 345 346


## 场景示例

  可使用弹性布局做出子元素排列方式为水平方向排列,且子元素的总宽度超出父元素的宽度不换行,子元素在水平方向两端对齐,中间间距由除首尾外的子元素平分,竖直方向上子元素居中的效果。

H
HelloCrease 已提交
347
```ts
K
kukixi 已提交
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
@Entry
@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%')
        .padding(10)
        .backgroundColor(0xAFEEEE)
      }.width('100%').margin({ top: 5 })
    }.width('100%') 
 }
}
```


![zh-cn_image_0000001261605867](figures/zh-cn_image_0000001261605867.png)
371 372 373 374 375 376


## 相关实例

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

377
- [弹性布局(eTS)(API8)](https://gitee.com/openharmony/codelabs/tree/master/ETSUI/FlowLayoutEts)
378 379

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