1# Flex布局
2
3>  **说明:**
4>  - 从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
5>
6>  - 仅当父组件是 [Flex](ts-container-flex.md)、[Column](ts-container-column.md)、[Row](ts-container-row.md)、[GridRow](ts-container-gridrow.md)(仅针对[alignSelf](#alignself))时生效。
7
8## flexBasis
9
10flexBasis(value: number | string)
11
12设置组件的基准尺寸。
13
14**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。
15
16**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
17
18**系统能力:** SystemCapability.ArkUI.ArkUI.Full
19
20**参数:**
21
22| 参数名 | 类型                       | 必填 | 说明                                                         |
23| ------ | -------------------------- | ---- | ------------------------------------------------------------ |
24| value  | number&nbsp;\|&nbsp;string | 是   | 设置组件在父容器主轴方向上的基准尺寸。<br/>默认值:'auto'(表示组件在主轴方向上的基准尺寸为组件原本的大小)。<br/>string类型可选值:可以转化为数字的字符串(如'10')或带长度单位的字符串(如'10px')或'auto',不允许设置百分比字符串。<br/>number:取值范围(0,+∞),单位为vp。<br/>异常值:默认为'auto'。 |
25
26## flexGrow
27
28flexGrow(value: number)
29
30设置组件在父容器的剩余空间所占比例。
31
32**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。
33
34**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
35
36**系统能力:** SystemCapability.ArkUI.ArkUI.Full
37
38**参数:**
39
40| 参数名 | 类型   | 必填 | 说明                                                         |
41| ------ | ------ | ---- | ------------------------------------------------------------ |
42| value  | number | 是   | 设置父容器在主轴方向上的剩余空间分配给此属性所在组件的比例。<br/>默认值:0 |
43
44## flexShrink
45
46flexShrink(value: number)
47
48设置父容器压缩尺寸分配给此属性所在组件的比例。当父容器为Column、Row时,需设置主轴方向的尺寸。
49
50**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。
51
52**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
53
54**系统能力:** SystemCapability.ArkUI.ArkUI.Full
55
56**参数:**
57
58| 参数名 | 类型   | 必填 | 说明                                                         |
59| ------ | ------ | ---- | ------------------------------------------------------------ |
60| value  | number | 是   | 设置父容器压缩尺寸分配给此属性所在组件的比例。<br/>父容器为[Column](ts-container-column.md)、[Row](ts-container-row.md)时,默认值:0<br/> 父容器为[Flex](ts-container-flex.md)时,默认值:1 <br/>[constraintSize](ts-universal-attributes-size.md#constraintsize)限制组件的尺寸范围,[Column](ts-container-column.md)和[Row](ts-container-row.md)即使设置了[constraintSize](ts-universal-attributes-size.md#constraintsize),在未设置主轴尺寸([width](ts-universal-attributes-size.md#width)/[height](ts-universal-attributes-size.md#height)/[size](ts-universal-attributes-size.md#size))时仍遵守默认布局行为,在主轴上自适应子组件尺寸,此时flexShrink不生效。|
61
62## alignSelf
63
64alignSelf(value: ItemAlign)
65
66子组件在父容器交叉轴的对齐格式。
67
68**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。
69
70**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
71
72**系统能力:** SystemCapability.ArkUI.ArkUI.Full
73
74**参数:**
75
76| 参数名 | 类型                                        | 必填 | 说明                                                         |
77| ------ | ------------------------------------------- | ---- | ------------------------------------------------------------ |
78| value  | [ItemAlign](ts-appendix-enums.md#itemalign) | 是   | 子组件在父容器交叉轴的对齐格式,会覆盖[Flex](ts-container-flex.md)、[Column](ts-container-column.md)、[Row](ts-container-row.md)、[GridRow](ts-container-gridrow.md)布局容器中的alignItems设置。<br/>[GridCol](./ts-container-gridcol.md)可以绑定alignsSelf属性来改变它自身在交叉轴方向上的布局。<br/>默认值:ItemAlign.Auto |
79
80
81## 示例
82
83通过配置flexBasis/flexGrow/flexShrink/alignSelf属性设置Flex布局。
84
85```ts
86// xxx.ets
87@Entry
88@Component
89struct FlexExample {
90  build() {
91    Column({ space: 5 }) {
92      Text('flexBasis').fontSize(9).fontColor(0xCCCCCC).width('90%')
93      // 基于主轴的基准尺寸
94      // flexBasis()值可以是字符串'auto',表示基准尺寸是元素本来的大小,也可以是长度设置,相当于.width()/.height()
95      Flex() {
96        Text('flexBasis(100)')
97          .flexBasis(100) // 这里表示宽度为100vp
98          .height(100)
99          .backgroundColor(0xF5DEB3)
100          .textAlign(TextAlign.Center)
101        Text(`flexBasis('auto')`)
102          .flexBasis('auto') // 这里表示宽度保持原本设置的60%的宽度
103          .width('60%')
104          .height(100)
105          .backgroundColor(0xD2B48C)
106          .textAlign(TextAlign.Center)
107      }.width('90%').height(120).padding(10).backgroundColor(0xAFEEEE)
108
109      Text('flexGrow').fontSize(9).fontColor(0xCCCCCC).width('90%')
110      // flexGrow()表示剩余空间分配给该元素的比例
111      Flex() {
112        Text('flexGrow(2)')
113          .flexGrow(2) // 父容器分配给该Text的宽度为剩余宽度的2/3
114          .height(100)
115          .backgroundColor(0xF5DEB3)
116          .textAlign(TextAlign.Center)
117        Text('flexGrow(1)')
118          .flexGrow(1) // 父容器分配给该Text的宽度为剩余宽度的1/3
119          .height(100)
120          .backgroundColor(0xD2B48C)
121          .textAlign(TextAlign.Center)
122      }.width('90%').height(120).padding(10).backgroundColor(0xAFEEEE)
123
124      Text('flexShrink').fontSize(9).fontColor(0xCCCCCC).width('90%')
125      // flexShrink()表示该元素的压缩比例,基于超出的总尺寸进行计算
126      // 第一个text压缩比例是0,另外两个都是1,因此放不下时等比例压缩后两个,第一个不压缩
127      Flex({ direction: FlexDirection.Row }) {
128        Text('flexShrink(0)')
129          .flexShrink(0)
130          .width('50%')
131          .height(100)
132          .backgroundColor(0xF5DEB3)
133          .textAlign(TextAlign.Center)
134        Text('default flexShrink') // 默认值为1
135          .width('40%')
136          .height(100)
137          .backgroundColor(0xD2B48C)
138          .textAlign(TextAlign.Center)
139        Text('flexShrink(1)')
140          .flexShrink(1)
141          .width('40%')
142          .height(100)
143          .backgroundColor(0xF5DEB3)
144          .textAlign(TextAlign.Center)
145      }.width('90%').height(120).padding(10).backgroundColor(0xAFEEEE)
146
147      Text('alignSelf').fontSize(9).fontColor(0xCCCCCC).width('90%')
148      // alignSelf会覆盖Flex布局容器中的alignItems设置
149      Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center }) {
150        Text('no alignSelf,height:70')
151          .width('33%')
152          .height(70)
153          .backgroundColor(0xF5DEB3)
154          .textAlign(TextAlign.Center)
155        Text('alignSelf End')
156          .alignSelf(ItemAlign.End)
157          .width('33%')
158          .height(70)
159          .backgroundColor(0xD2B48C)
160          .textAlign(TextAlign.Center)
161        Text('no alignSelf,height:100%')
162          .width('34%')
163          .height('100%')
164          .backgroundColor(0xF5DEB3)
165          .textAlign(TextAlign.Center)
166      }.width('90%').height(120).padding(10).backgroundColor(0xAFEEEE)
167    }.width('100%').margin({ top: 5 })
168  }
169}
170```
171
172![flex](figures/flex.PNG)
173