• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Flex Layout
2
3>  **NOTE**
4>  - The initial APIs of this module are supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
5>
6>  - The flex layout is valid only when the parent container is a [Flex](ts-container-flex.md), [Column](ts-container-column.md), [Row](ts-container-row.md), or [GridRow](ts-container-gridrow.md) (only for [alignSelf](#alignself)) component.
7
8## flexBasis
9
10flexBasis(value: number | string): T
11
12Sets the base size of the component.
13
14**Widget capability**: This API can be used in ArkTS widgets since API version 9.
15
16**Atomic service API**: This API can be used in atomic services since API version 11.
17
18**System capability**: SystemCapability.ArkUI.ArkUI.Full
19
20**Parameters**
21
22| Name| Type                      | Mandatory| Description                                                        |
23| ------ | -------------------------- | ---- | ------------------------------------------------------------ |
24| value  | number \| string | Yes  | Base size of the component in the main axis of the parent container.<br>Default value: **'auto'** (indicating that the base size of the component in the main axis is the original size of the component)<br>For the string type, the value must be a string that can be converted into a number (for example, **'10'**), a string that includes a length unit (for example, **'10px'**), or the literal string **'auto'**; percentage-based strings are not supported.<br>For the number type, the value range is (0, +∞), and the unit is vp.<br>Invalid values are treated as the default value **'auto'**.|
25
26**Return value**
27
28| Type| Description|
29| --- | --- |
30|  T | Current component.|
31
32## flexGrow
33
34flexGrow(value: number): T
35
36Sets the percentage of the parent container's remaining space that is allocated to the component.
37
38**Widget capability**: This API can be used in ArkTS widgets since API version 9.
39
40**Atomic service API**: This API can be used in atomic services since API version 11.
41
42**System capability**: SystemCapability.ArkUI.ArkUI.Full
43
44**Parameters**
45
46| Name| Type  | Mandatory| Description                                                        |
47| ------ | ------ | ---- | ------------------------------------------------------------ |
48| value  | number | Yes  | Percentage of the parent container's remaining space that is allocated to the component.<br>Value range: (0, +∞).<br>Default value: **0**.|
49
50**Return value**
51
52| Type| Description|
53| --- | --- |
54|  T | Current component.|
55
56## flexShrink
57
58flexShrink(value: number): T
59
60Sets the percentage of the parent container's shrink size that is allocated to the component. When the parent container is **Column** or **Row**, you must set the size along the main axis.
61
62**Widget capability**: This API can be used in ArkTS widgets since API version 9.
63
64**Atomic service API**: This API can be used in atomic services since API version 11.
65
66**System capability**: SystemCapability.ArkUI.ArkUI.Full
67
68**Parameters**
69
70| Name| Type  | Mandatory| Description                                                        |
71| ------ | ------ | ---- | ------------------------------------------------------------ |
72| value  | number | Yes  | Percentage of the parent container's shrink size that is allocated to the component.<br>If the parent container is [Column](ts-container-column.md) or [Row](ts-container-row.md), the default value is **0**, and the value range is (0, +∞).<br> If the parent container is [Flex](ts-container-flex.md), the default value is **1**.<br>[constraintSize](ts-universal-attributes-size.md#constraintsize) limits the component's size range. For [Column](ts-container-column.md) and [Row](ts-container-row.md) components without explicit main axis size specified (through [width](ts-universal-attributes-size.md#width), [height](ts-universal-attributes-size.md#height), or [size](ts-universal-attributes-size.md#size)), the default layout behavior (adapt-to-fit child components) applies, and **flexShrink** has no effect.|
73
74**Return value**
75
76| Type| Description|
77| --- | --- |
78|  T | Current component.|
79
80## alignSelf
81
82alignSelf(value: ItemAlign): T
83
84Sets the alignment mode of the child components along the cross axis of the parent container.
85
86**Widget capability**: This API can be used in ArkTS widgets since API version 9.
87
88**Atomic service API**: This API can be used in atomic services since API version 11.
89
90**System capability**: SystemCapability.ArkUI.ArkUI.Full
91
92**Parameters**
93
94| Name| Type                                       | Mandatory| Description                                                        |
95| ------ | ------------------------------------------- | ---- | ------------------------------------------------------------ |
96| value  | [ItemAlign](ts-appendix-enums.md#itemalign) | Yes  | Alignment mode of the child components along the cross axis of the parent container. The setting overwrites the **alignItems** setting of the parent container ([Flex](ts-container-flex.md), [Column](ts-container-column.md), [Row](ts-container-row.md), or [GridRow](ts-container-gridrow.md)).<br>[GridCol](./ts-container-gridcol.md) can have the **alignSelf** attribute bound to change its own layout along the cross axis.<br>Default value: **ItemAlign.Auto**|
97
98**Return value**
99
100| Type| Description|
101| --- | --- |
102|  T | Current component.|
103
104## Example
105
106This example shows how to set up a flex layout through the **flexBasis**, **flexGrow**, **flexShrink**, and **alignSelf** attributes.
107
108```ts
109// xxx.ets
110@Entry
111@Component
112struct FlexExample {
113  build() {
114    Column({ space: 5 }) {
115      Text('flexBasis').fontSize(9).fontColor(0xCCCCCC).width('90%')
116      // Base size in the main axis
117      // The value of flexBasis() can be 'auto' or a number, which is equivalent to .width()/.height().
118      Flex() {
119        Text('flexBasis(100)')
120          .flexBasis(100) // The width is 100 vp.
121          .height(100)
122          .backgroundColor(0xF5DEB3)
123          .textAlign(TextAlign.Center)
124        Text(`flexBasis('auto')`)
125          .flexBasis('auto') // The width is 60% of the original width.
126          .width('60%')
127          .height(100)
128          .backgroundColor(0xD2B48C)
129          .textAlign(TextAlign.Center)
130      }.width('90%').height(120).padding(10).backgroundColor(0xAFEEEE)
131
132      Text('flexGrow').fontSize(9).fontColor(0xCCCCCC).width('90%')
133      // flexGrow() indicates the percentage of the remaining space allocated to the component.
134      Flex() {
135        Text('flexGrow(2)')
136          .flexGrow(2) // The width allocated to the Text component is 2/3 of the remaining width of the parent container.
137          .height(100)
138          .backgroundColor(0xF5DEB3)
139          .textAlign(TextAlign.Center)
140        Text('flexGrow(1)')
141          .flexGrow(1) // The width allocated to the Text component is 1/3 of the remaining width of the parent container.
142          .height(100)
143          .backgroundColor(0xD2B48C)
144          .textAlign(TextAlign.Center)
145      }.width('90%').height(120).padding(10).backgroundColor(0xAFEEEE)
146
147      Text('flexShrink').fontSize(9).fontColor(0xCCCCCC).width('90%')
148      // flexShrink() indicates the percentage of the shrink size allocated to the component.
149      // The value is 0 for the first Text component and 1 for the other two Text components. This means that, if the components cannot be completely displayed in the parent container, the latter two are shrunk proportionally, while the former is not shrunk.
150      Flex({ direction: FlexDirection.Row }) {
151        Text('flexShrink(0)')
152          .flexShrink(0)
153          .width('50%')
154          .height(100)
155          .backgroundColor(0xF5DEB3)
156          .textAlign(TextAlign.Center)
157        Text('default flexShrink') // The default value is 1.
158          .width('40%')
159          .height(100)
160          .backgroundColor(0xD2B48C)
161          .textAlign(TextAlign.Center)
162        Text('flexShrink(1)')
163          .flexShrink(1)
164          .width('40%')
165          .height(100)
166          .backgroundColor(0xF5DEB3)
167          .textAlign(TextAlign.Center)
168      }.width('90%').height(120).padding(10).backgroundColor(0xAFEEEE)
169
170      Text('alignSelf').fontSize(9).fontColor(0xCCCCCC).width('90%')
171      // The alignSelf setting overrides the alignItems setting of the parent container.
172      Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center }) {
173        Text('no alignSelf,height:70')
174          .width('33%')
175          .height(70)
176          .backgroundColor(0xF5DEB3)
177          .textAlign(TextAlign.Center)
178        Text('alignSelf End')
179          .alignSelf(ItemAlign.End)
180          .width('33%')
181          .height(70)
182          .backgroundColor(0xD2B48C)
183          .textAlign(TextAlign.Center)
184        Text('no alignSelf,height:100%')
185          .width('34%')
186          .height('100%')
187          .backgroundColor(0xF5DEB3)
188          .textAlign(TextAlign.Center)
189      }.width('90%').height(120).padding(10).backgroundColor(0xAFEEEE)
190    }.width('100%').margin({ top: 5 })
191  }
192}
193```
194
195![flex](figures/flex.PNG)
196