1e41f4b71Sopenharmony_ci# Custom Component Layout 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe custom layout of a custom component is used to lay out its child components through data calculation. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **NOTE** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci## onPlaceChildren<sup>10+</sup> 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_cionPlaceChildren?(selfLayoutInfo: GeometryInfo, children: Array<Layoutable>, constraint: ConstraintSizeOptions):void 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ciInvoked when the custom component lays out its child components. Through this callback the component receives its child component size constraints from the ArkUI framework. State variables should not be changed in this callback. 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci**Parameters** 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci| Name | Type | Description | 22e41f4b71Sopenharmony_ci|----------------|------------------------------------------------------------|------------------| 23e41f4b71Sopenharmony_ci| selfLayoutInfo | [GeometryInfo](#geometryinfo10) | Layout information of the parent component. | 24e41f4b71Sopenharmony_ci| children | Array<[Layoutable](#layoutable10)> | Child component layout information. | 25e41f4b71Sopenharmony_ci| constraint | [ConstraintSizeOptions](ts-types.md#constraintsizeoptions) | Size constraint of the parent component.| 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci## onMeasureSize<sup>10+</sup> 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_cionMeasureSize?(selfLayoutInfo: GeometryInfo, children: Array<Measurable>, constraint: ConstraintSizeOptions): SizeResult 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ciInvoked when the custom component needs to determine its size. Through this callback the component receives its child component layout information and size constraint from the ArkUI framework. State variables should not be changed in this callback. 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci**Parameters** 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci| Name | Type | Description | 40e41f4b71Sopenharmony_ci| -------------- | ---------------------------------------------------------- | ------------------------------------------------------------ | 41e41f4b71Sopenharmony_ci| selfLayoutInfo | [GeometryInfo](#geometryinfo10) | Layout information of the parent component. | 42e41f4b71Sopenharmony_ci| children | Array<[Measurable](#measurable10)> | Child component layout information.<br>**NOTE**<br>When a child component does not have its layout information set, it retains the previous layout settings or, if no previous layout settings are available, stays at the default size of 0.| 43e41f4b71Sopenharmony_ci| constraint | [ConstraintSizeOptions](ts-types.md#constraintsizeoptions) | Size constraint of the parent component. | 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci**Return value** 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci| Type | Description | 48e41f4b71Sopenharmony_ci| --------------------------- | -------------- | 49e41f4b71Sopenharmony_ci| [SizeResult](#sizeresult10) | Component size information.| 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci## GeometryInfo<sup>10+</sup> 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ciProvides the parent component layout information. Inherits from [SizeResult](#sizeresult10). 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci| Name | Type | Description | 60e41f4b71Sopenharmony_ci|-------------|-----------|---------------------| 61e41f4b71Sopenharmony_ci| borderWidth | [EdgeWidth](ts-types.md#edgewidths9) | Border width of the parent component.<br>Unit: vp | 62e41f4b71Sopenharmony_ci| margin | [Margin](ts-types.md#margin) | Margin of the parent component.<br>Unit: vp | 63e41f4b71Sopenharmony_ci| padding | [Padding](ts-types.md#padding) | Padding of the parent component.<br>Unit: vp| 64e41f4b71Sopenharmony_ci| width | number | Width obtained from the measurement result.<br>Unit: vp<br> **NOTE**<br>If the value is empty, the component width in percentage is returned.| 65e41f4b71Sopenharmony_ci| height | number | Height obtained from the measurement result.<br>Unit: vp<br> **NOTE**<br>If the value is empty, the component height in percentage is returned.| 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci## Layoutable<sup>10+</sup> 69e41f4b71Sopenharmony_ci 70e41f4b71Sopenharmony_ciProvides the child component layout information. 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ci### Name 75e41f4b71Sopenharmony_ci 76e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 77e41f4b71Sopenharmony_ci|--------------|---------------------------------- | -----------------------------------------------|---------------------| 78e41f4b71Sopenharmony_ci| measureResult| [MeasureResult](#measureresult10) | Yes| Measurement result of the child component. Inherits from [SizeResult](#sizeresult10).<br>**Atomic service API**: This API can be used in atomic services since API version 11.<br>Unit: vp | 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci### layout 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_cilayout(position: Position) 84e41f4b71Sopenharmony_ci 85e41f4b71Sopenharmony_ciApplies the specified position information to the child component. 86e41f4b71Sopenharmony_ci 87e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 88e41f4b71Sopenharmony_ci 89e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci**Parameters** 92e41f4b71Sopenharmony_ci 93e41f4b71Sopenharmony_ci| Name | Type | Mandatory |Description | 94e41f4b71Sopenharmony_ci|-----------------|---------------------------------------------------------|---------------------|-------------| 95e41f4b71Sopenharmony_ci| position | [Position](ts-types.md#position) | Yes | Position. | 96e41f4b71Sopenharmony_ci 97e41f4b71Sopenharmony_ci### getMargin<sup>12+</sup> 98e41f4b71Sopenharmony_ci 99e41f4b71Sopenharmony_cigetMargin() : DirectionalEdgesT\<number> 100e41f4b71Sopenharmony_ci 101e41f4b71Sopenharmony_ciObtains the margin of the child component. 102e41f4b71Sopenharmony_ci 103e41f4b71Sopenharmony_ci**Return value** 104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ci| Type | Description | 106e41f4b71Sopenharmony_ci|------------------------------------|---------------------------------------------| 107e41f4b71Sopenharmony_ci| [DirectionalEdgesT<number>](#directionaledgestt12) | Margin of the child component. | 108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_ci ### getPadding<sup>12+</sup> 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_cigetPadding() : DirectionalEdgesT\<number> 112e41f4b71Sopenharmony_ci 113e41f4b71Sopenharmony_ci Obtains the padding of the child component. 114e41f4b71Sopenharmony_ci 115e41f4b71Sopenharmony_ci **Return value** 116e41f4b71Sopenharmony_ci 117e41f4b71Sopenharmony_ci| Type | Description | 118e41f4b71Sopenharmony_ci|------------------------------------|---------------------------------------------| 119e41f4b71Sopenharmony_ci| [DirectionalEdgesT<number>](#directionaledgestt12) | Padding of the child component. | 120e41f4b71Sopenharmony_ci 121e41f4b71Sopenharmony_ci### getBorderWidth<sup>12+</sup> 122e41f4b71Sopenharmony_ci 123e41f4b71Sopenharmony_cigetBorderWidth() : DirectionalEdgesT\<number> 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_ciObtains the border width of the child component. 126e41f4b71Sopenharmony_ci 127e41f4b71Sopenharmony_ci**Return value** 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ci| Type | Description | 130e41f4b71Sopenharmony_ci|------------------------------------|---------------------------------------------| 131e41f4b71Sopenharmony_ci| [DirectionalEdgesT<number>](#directionaledgestt12) | Border width of the child component. | 132e41f4b71Sopenharmony_ci 133e41f4b71Sopenharmony_ci## Measurable<sup>10+</sup> 134e41f4b71Sopenharmony_ci 135e41f4b71Sopenharmony_ciProvides the child component position information. 136e41f4b71Sopenharmony_ci 137e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 138e41f4b71Sopenharmony_ci 139e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 140e41f4b71Sopenharmony_ci 141e41f4b71Sopenharmony_ci### measure 142e41f4b71Sopenharmony_ci 143e41f4b71Sopenharmony_ci measure(constraint: ConstraintSizeOptions) : MeasureResult 144e41f4b71Sopenharmony_ci 145e41f4b71Sopenharmony_ci Applies the size constraint to the child component. 146e41f4b71Sopenharmony_ci 147e41f4b71Sopenharmony_ci **System capability**: SystemCapability.ArkUI.ArkUI.Full 148e41f4b71Sopenharmony_ci 149e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 150e41f4b71Sopenharmony_ci 151e41f4b71Sopenharmony_ci**Parameters** 152e41f4b71Sopenharmony_ci 153e41f4b71Sopenharmony_ci| Name | Type | Mandatory |Description | 154e41f4b71Sopenharmony_ci|-----------------|---------------------------------------------------------|---------------------|-------------| 155e41f4b71Sopenharmony_ci| constraint | [ConstraintSizeOptions](ts-types.md#constraintsizeoptions) | Yes | Size constraint. | 156e41f4b71Sopenharmony_ci 157e41f4b71Sopenharmony_ci**Return value** 158e41f4b71Sopenharmony_ci 159e41f4b71Sopenharmony_ci | Type | Description | 160e41f4b71Sopenharmony_ci |------------------------------------|-------------------------| 161e41f4b71Sopenharmony_ci |[MeasureResult](#measureresult10) | Provides the measurement result of the component. | 162e41f4b71Sopenharmony_ci 163e41f4b71Sopenharmony_ci ### getMargin<sup>12+</sup> 164e41f4b71Sopenharmony_ci 165e41f4b71Sopenharmony_ci getMargin() : DirectionalEdgesT\<number\> 166e41f4b71Sopenharmony_ci 167e41f4b71Sopenharmony_ci Obtains the padding of the child component. 168e41f4b71Sopenharmony_ci 169e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 170e41f4b71Sopenharmony_ci 171e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 172e41f4b71Sopenharmony_ci 173e41f4b71Sopenharmony_ci**Return value** 174e41f4b71Sopenharmony_ci 175e41f4b71Sopenharmony_ci | Type | Description | 176e41f4b71Sopenharmony_ci |------------------------------------|-------------------------| 177e41f4b71Sopenharmony_ci |[DirectionalEdgesT<number>](#directionaledgestt12) | Margin of the child component. | 178e41f4b71Sopenharmony_ci 179e41f4b71Sopenharmony_ci### getPadding<sup>12+</sup> 180e41f4b71Sopenharmony_ci 181e41f4b71Sopenharmony_cigetPadding() : DirectionalEdgesT\<number\> 182e41f4b71Sopenharmony_ci 183e41f4b71Sopenharmony_ciObtains the padding of the child component. 184e41f4b71Sopenharmony_ci 185e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 186e41f4b71Sopenharmony_ci 187e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 188e41f4b71Sopenharmony_ci 189e41f4b71Sopenharmony_ci**Return value** 190e41f4b71Sopenharmony_ci 191e41f4b71Sopenharmony_ci | Type | Description | 192e41f4b71Sopenharmony_ci |------------------------------------|-------------------------| 193e41f4b71Sopenharmony_ci |[DirectionalEdgesT<number>](#directionaledgestt12) | Padding of the child component. | 194e41f4b71Sopenharmony_ci 195e41f4b71Sopenharmony_ci ### getBorderWidth<sup>12+</sup> 196e41f4b71Sopenharmony_ci 197e41f4b71Sopenharmony_cigetBorderWidth() : DirectionalEdgesT\<number\> 198e41f4b71Sopenharmony_ci 199e41f4b71Sopenharmony_ciObtains the border widthof the child component. 200e41f4b71Sopenharmony_ci 201e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 202e41f4b71Sopenharmony_ci 203e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 204e41f4b71Sopenharmony_ci 205e41f4b71Sopenharmony_ci**Return value** 206e41f4b71Sopenharmony_ci 207e41f4b71Sopenharmony_ci | Type | Description | 208e41f4b71Sopenharmony_ci |------------------------------------|-------------------------| 209e41f4b71Sopenharmony_ci |[DirectionalEdgesT<number>](#directionaledgestt12) | Border width of the child component.| 210e41f4b71Sopenharmony_ci 211e41f4b71Sopenharmony_ci 212e41f4b71Sopenharmony_ci## MeasureResult<sup>10+</sup> 213e41f4b71Sopenharmony_ci 214e41f4b71Sopenharmony_ciProvides the measurement result of the component. 215e41f4b71Sopenharmony_ci 216e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 217e41f4b71Sopenharmony_ci 218e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 219e41f4b71Sopenharmony_ci 220e41f4b71Sopenharmony_ci| Name | Type | Description | 221e41f4b71Sopenharmony_ci|--------|--------|-------| 222e41f4b71Sopenharmony_ci| width | number | Width obtained from the measurement result.<br>Unit: vp| 223e41f4b71Sopenharmony_ci| height | number | Height obtained from the measurement result.<br>Unit: vp| 224e41f4b71Sopenharmony_ci 225e41f4b71Sopenharmony_ci 226e41f4b71Sopenharmony_ci## SizeResult<sup>10+</sup> 227e41f4b71Sopenharmony_ci 228e41f4b71Sopenharmony_ciProvides the component size information. 229e41f4b71Sopenharmony_ci 230e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 231e41f4b71Sopenharmony_ci 232e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 233e41f4b71Sopenharmony_ci 234e41f4b71Sopenharmony_ci| Name | Type | Description | 235e41f4b71Sopenharmony_ci|--------|--------|-------| 236e41f4b71Sopenharmony_ci| width | number | Width obtained from the measurement result.<br>Unit: vp| 237e41f4b71Sopenharmony_ci| height | number | Height obtained from the measurement result.<br>Unit: vp| 238e41f4b71Sopenharmony_ci 239e41f4b71Sopenharmony_ci## DirectionalEdgesT\<T><sup>12+</sup> 240e41f4b71Sopenharmony_ci 241e41f4b71Sopenharmony_ciDefines the directional edges. 242e41f4b71Sopenharmony_ci 243e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 244e41f4b71Sopenharmony_ci 245e41f4b71Sopenharmony_ci**Widget capability**: This API can be used in ArkTS widgets since API version 12. 246e41f4b71Sopenharmony_ci 247e41f4b71Sopenharmony_ci| Name | Type| Description | 248e41f4b71Sopenharmony_ci| ------ | ---- | ---------------- | 249e41f4b71Sopenharmony_ci| start | T | Start edge. It is the left edge if the direction is left-to-right and the right edge if the direction is right-to-left.| 250e41f4b71Sopenharmony_ci| end | T | End edge. It is the right edge if the direction is left-to-right and the left edge if the direction is right-to-left.| 251e41f4b71Sopenharmony_ci| top | T | Top edge.| 252e41f4b71Sopenharmony_ci| bottom | T | Top edge.| 253e41f4b71Sopenharmony_ci 254e41f4b71Sopenharmony_ci> **NOTE** 255e41f4b71Sopenharmony_ci> 256e41f4b71Sopenharmony_ci>- The custom layout does not support the LazyForEach syntax. 257e41f4b71Sopenharmony_ci>- When a custom layout is created in builder mode, only **this.builder()** is allowed in the **build()** method of a custom component, as shown in the recommended usage in the example below. 258e41f4b71Sopenharmony_ci>- The size parameters of the parent component (custom component), except **aspectRatio**, are at a lower priority than those specified by **onMeasureSize**. 259e41f4b71Sopenharmony_ci>- The position parameters of the child component, except **offset**, **position**, and **markAnchor**, are at a lower priority than those specified by **onPlaceChildren**, and do not take effect. 260e41f4b71Sopenharmony_ci>- When using the custom layout method, you must call **onMeasureSize** and **onPlaceChildren** at the same time for the layout to display properly. 261e41f4b71Sopenharmony_ci 262e41f4b71Sopenharmony_ci**Example 1** 263e41f4b71Sopenharmony_ciThis example demonstrates how to customize a layout. 264e41f4b71Sopenharmony_ci``` 265e41f4b71Sopenharmony_ci// xxx.ets 266e41f4b71Sopenharmony_ci@Entry 267e41f4b71Sopenharmony_ci@Component 268e41f4b71Sopenharmony_cistruct Index { 269e41f4b71Sopenharmony_ci build() { 270e41f4b71Sopenharmony_ci Column() { 271e41f4b71Sopenharmony_ci CustomLayout({ builder: ColumnChildren }) 272e41f4b71Sopenharmony_ci } 273e41f4b71Sopenharmony_ci } 274e41f4b71Sopenharmony_ci} 275e41f4b71Sopenharmony_ci 276e41f4b71Sopenharmony_ci@Builder 277e41f4b71Sopenharmony_cifunction ColumnChildren() { 278e41f4b71Sopenharmony_ci ForEach([1, 2, 3], (index: number) => {// LazyForEach is not supported. 279e41f4b71Sopenharmony_ci Text('S' + index) 280e41f4b71Sopenharmony_ci .fontSize(30) 281e41f4b71Sopenharmony_ci .width(100) 282e41f4b71Sopenharmony_ci .height(100) 283e41f4b71Sopenharmony_ci .borderWidth(2) 284e41f4b71Sopenharmony_ci .offset({ x: 10, y: 20 }) 285e41f4b71Sopenharmony_ci }) 286e41f4b71Sopenharmony_ci} 287e41f4b71Sopenharmony_ci 288e41f4b71Sopenharmony_ci@Component 289e41f4b71Sopenharmony_cistruct CustomLayout { 290e41f4b71Sopenharmony_ci @Builder 291e41f4b71Sopenharmony_ci doNothingBuilder() { 292e41f4b71Sopenharmony_ci }; 293e41f4b71Sopenharmony_ci 294e41f4b71Sopenharmony_ci @BuilderParam builder: () => void = this.doNothingBuilder; 295e41f4b71Sopenharmony_ci @State startSize: number = 100; 296e41f4b71Sopenharmony_ci result: SizeResult = { 297e41f4b71Sopenharmony_ci width: 0, 298e41f4b71Sopenharmony_ci height: 0 299e41f4b71Sopenharmony_ci }; 300e41f4b71Sopenharmony_ci 301e41f4b71Sopenharmony_ci onPlaceChildren(selfLayoutInfo: GeometryInfo, children: Array<Layoutable>, constraint: ConstraintSizeOptions) { 302e41f4b71Sopenharmony_ci let startPos = 300; 303e41f4b71Sopenharmony_ci children.forEach((child) => { 304e41f4b71Sopenharmony_ci let pos = startPos - child.measureResult.height; 305e41f4b71Sopenharmony_ci child.layout({ x: pos, y: pos }) 306e41f4b71Sopenharmony_ci }) 307e41f4b71Sopenharmony_ci } 308e41f4b71Sopenharmony_ci 309e41f4b71Sopenharmony_ci onMeasureSize(selfLayoutInfo: GeometryInfo, children: Array<Measurable>, constraint: ConstraintSizeOptions) { 310e41f4b71Sopenharmony_ci let size = 100; 311e41f4b71Sopenharmony_ci children.forEach((child) => { 312e41f4b71Sopenharmony_ci let result: MeasureResult = child.measure({ minHeight: size, minWidth: size, maxWidth: size, maxHeight: size }) 313e41f4b71Sopenharmony_ci size += result.width / 2 314e41f4b71Sopenharmony_ci ; 315e41f4b71Sopenharmony_ci }) 316e41f4b71Sopenharmony_ci this.result.width = 100; 317e41f4b71Sopenharmony_ci this.result.height = 400; 318e41f4b71Sopenharmony_ci return this.result; 319e41f4b71Sopenharmony_ci } 320e41f4b71Sopenharmony_ci 321e41f4b71Sopenharmony_ci build() { 322e41f4b71Sopenharmony_ci this.builder() 323e41f4b71Sopenharmony_ci } 324e41f4b71Sopenharmony_ci} 325e41f4b71Sopenharmony_ci``` 326e41f4b71Sopenharmony_ci 327e41f4b71Sopenharmony_ci 328e41f4b71Sopenharmony_ci 329e41f4b71Sopenharmony_ci**Example 2** 330e41f4b71Sopenharmony_ciThis example shows how to determine whether a component participates in layout calculation based on its position. 331e41f4b71Sopenharmony_ci``` 332e41f4b71Sopenharmony_ci// xxx.ets 333e41f4b71Sopenharmony_ci@Entry 334e41f4b71Sopenharmony_ci@Component 335e41f4b71Sopenharmony_cistruct Index { 336e41f4b71Sopenharmony_ci build() { 337e41f4b71Sopenharmony_ci Column() { 338e41f4b71Sopenharmony_ci CustomLayout({ builder: ColumnChildren }) 339e41f4b71Sopenharmony_ci } 340e41f4b71Sopenharmony_ci .justifyContent(FlexAlign.Center) 341e41f4b71Sopenharmony_ci .width("100%") 342e41f4b71Sopenharmony_ci .height("100%") 343e41f4b71Sopenharmony_ci } 344e41f4b71Sopenharmony_ci} 345e41f4b71Sopenharmony_ci 346e41f4b71Sopenharmony_ci@Builder 347e41f4b71Sopenharmony_cifunction ColumnChildren() { 348e41f4b71Sopenharmony_ci ForEach([1, 2, 3], (item: number, index: number) => { // LazyForEach is not supported. 349e41f4b71Sopenharmony_ci Text('S' + item) 350e41f4b71Sopenharmony_ci .fontSize(20) 351e41f4b71Sopenharmony_ci .width(60 + 10 * index) 352e41f4b71Sopenharmony_ci .height(100) 353e41f4b71Sopenharmony_ci .borderWidth(2) 354e41f4b71Sopenharmony_ci .margin({ left:10 }) 355e41f4b71Sopenharmony_ci .padding(10) 356e41f4b71Sopenharmony_ci }) 357e41f4b71Sopenharmony_ci} 358e41f4b71Sopenharmony_ci 359e41f4b71Sopenharmony_ci@Component 360e41f4b71Sopenharmony_cistruct CustomLayout { 361e41f4b71Sopenharmony_ci // Lay out only one row, and hide child components that are too large for the available space. 362e41f4b71Sopenharmony_ci @Builder 363e41f4b71Sopenharmony_ci doNothingBuilder() { 364e41f4b71Sopenharmony_ci }; 365e41f4b71Sopenharmony_ci 366e41f4b71Sopenharmony_ci @BuilderParam builder: () => void = this.doNothingBuilder; 367e41f4b71Sopenharmony_ci result: SizeResult = { 368e41f4b71Sopenharmony_ci width: 0, 369e41f4b71Sopenharmony_ci height: 0 370e41f4b71Sopenharmony_ci }; 371e41f4b71Sopenharmony_ci overFlowIndex: number = -1; 372e41f4b71Sopenharmony_ci 373e41f4b71Sopenharmony_ci onPlaceChildren(selfLayoutInfo: GeometryInfo, children: Array<Layoutable>, constraint: ConstraintSizeOptions) { 374e41f4b71Sopenharmony_ci let currentX = 0; 375e41f4b71Sopenharmony_ci let infinity = 100000; 376e41f4b71Sopenharmony_ci if (this.overFlowIndex == -1) { 377e41f4b71Sopenharmony_ci this.overFlowIndex = children.length; 378e41f4b71Sopenharmony_ci } 379e41f4b71Sopenharmony_ci for (let index = 0; index < children.length; ++index) { 380e41f4b71Sopenharmony_ci let child = children[index]; 381e41f4b71Sopenharmony_ci if (index >= this.overFlowIndex) { 382e41f4b71Sopenharmony_ci // Hide any child component that extends beyond the area of its parent component by placing it in a distant position. 383e41f4b71Sopenharmony_ci child.layout({x: infinity, y: 0}); 384e41f4b71Sopenharmony_ci continue; 385e41f4b71Sopenharmony_ci } 386e41f4b71Sopenharmony_ci child.layout({ x: currentX, y: 0 }) 387e41f4b71Sopenharmony_ci let margin = child.getMargin(); 388e41f4b71Sopenharmony_ci currentX += child.measureResult.width + margin.start + margin.end; 389e41f4b71Sopenharmony_ci } 390e41f4b71Sopenharmony_ci } 391e41f4b71Sopenharmony_ci 392e41f4b71Sopenharmony_ci onMeasureSize(selfLayoutInfo: GeometryInfo, children: Array<Measurable>, constraint: ConstraintSizeOptions) { 393e41f4b71Sopenharmony_ci let width = 0; 394e41f4b71Sopenharmony_ci let height = 0; 395e41f4b71Sopenharmony_ci this.overFlowIndex = -1; 396e41f4b71Sopenharmony_ci // Assume that the component width cannot exceed 200 vp or the maximum constraint. 397e41f4b71Sopenharmony_ci let maxWidth = Math.min(200, constraint.maxWidth as number); 398e41f4b71Sopenharmony_ci for (let index = 0; index < children.length; ++index) { 399e41f4b71Sopenharmony_ci let child = children[index]; 400e41f4b71Sopenharmony_ci let childResult: MeasureResult = child.measure({ 401e41f4b71Sopenharmony_ci minHeight: constraint.minHeight, 402e41f4b71Sopenharmony_ci minWidth: constraint.minWidth, 403e41f4b71Sopenharmony_ci maxWidth: constraint.maxWidth, 404e41f4b71Sopenharmony_ci maxHeight: constraint.maxHeight 405e41f4b71Sopenharmony_ci }) 406e41f4b71Sopenharmony_ci let margin = child.getMargin(); 407e41f4b71Sopenharmony_ci let newWidth = width + childResult.width + margin.start + margin.end; 408e41f4b71Sopenharmony_ci if (newWidth > maxWidth) { 409e41f4b71Sopenharmony_ci // Record the index of the component that should not be laid out. 410e41f4b71Sopenharmony_ci this.overFlowIndex = index; 411e41f4b71Sopenharmony_ci break; 412e41f4b71Sopenharmony_ci } 413e41f4b71Sopenharmony_ci // Accumulate the width and height of the parent component. 414e41f4b71Sopenharmony_ci width = newWidth; 415e41f4b71Sopenharmony_ci height = Math.max(height, childResult.height + margin.top + margin.bottom); 416e41f4b71Sopenharmony_ci } 417e41f4b71Sopenharmony_ci this.result.width = width; 418e41f4b71Sopenharmony_ci this.result.height = height; 419e41f4b71Sopenharmony_ci return this.result; 420e41f4b71Sopenharmony_ci } 421e41f4b71Sopenharmony_ci 422e41f4b71Sopenharmony_ci build() { 423e41f4b71Sopenharmony_ci this.builder() 424e41f4b71Sopenharmony_ci } 425e41f4b71Sopenharmony_ci} 426e41f4b71Sopenharmony_ci``` 427e41f4b71Sopenharmony_ci 428e41f4b71Sopenharmony_ci 429e41f4b71Sopenharmony_ci 430e41f4b71Sopenharmony_ci## onLayout<sup>(deprecated)</sup> 431e41f4b71Sopenharmony_ci 432e41f4b71Sopenharmony_cionLayout?(children: Array<LayoutChild>, constraint: ConstraintSizeOptions): void 433e41f4b71Sopenharmony_ci 434e41f4b71Sopenharmony_ciInvoked when the custom component lays out its child components. Through this callback the component receives its child component layout information and size constraint from the ArkUI framework. State variables should not be changed in this callback. 435e41f4b71Sopenharmony_ci 436e41f4b71Sopenharmony_ciThis API is supported since API version 9 and deprecated since API version 10. You are advised to use [onPlaceChildren](#onplacechildren10) instead. 437e41f4b71Sopenharmony_ci 438e41f4b71Sopenharmony_ci**Parameters** 439e41f4b71Sopenharmony_ci 440e41f4b71Sopenharmony_ci| Name | Type | Description | 441e41f4b71Sopenharmony_ci|------------|------------------------------------------------------------|------------------| 442e41f4b71Sopenharmony_ci| children | Array<[LayoutChild](#layoutchilddeprecated)> | Child component layout information. | 443e41f4b71Sopenharmony_ci| constraint | [ConstraintSizeOptions](ts-types.md#constraintsizeoptions) | Size constraint of the parent component.| 444e41f4b71Sopenharmony_ci 445e41f4b71Sopenharmony_ci## onMeasure<sup>(deprecated)</sup> 446e41f4b71Sopenharmony_ci 447e41f4b71Sopenharmony_cionMeasure?(children: Array<LayoutChild>, constraint: ConstraintSizeOptions): void 448e41f4b71Sopenharmony_ci 449e41f4b71Sopenharmony_ciInvoked when the custom component needs to determine its size. Through this callback the component receives its child component layout information and size constraint from the ArkUI framework. State variables should not be changed in this callback. 450e41f4b71Sopenharmony_ci 451e41f4b71Sopenharmony_ciThis API is supported since API version 9 and deprecated since API version 10. You are advised to use [onMeasureSize](#onmeasuresize10) instead. 452e41f4b71Sopenharmony_ci 453e41f4b71Sopenharmony_ci**Parameters** 454e41f4b71Sopenharmony_ci 455e41f4b71Sopenharmony_ci| Name | Type | Description | 456e41f4b71Sopenharmony_ci|------------|------------------------------------------------------------|------------------| 457e41f4b71Sopenharmony_ci| children | Array<[LayoutChild](#layoutchilddeprecated)> | Child component layout information. | 458e41f4b71Sopenharmony_ci| constraint | [ConstraintSizeOptions](ts-types.md#constraintsizeoptions) | Size constraint of the parent component.| 459e41f4b71Sopenharmony_ci 460e41f4b71Sopenharmony_ci## LayoutChild<sup>(deprecated)</sup> 461e41f4b71Sopenharmony_ci 462e41f4b71Sopenharmony_ciChild component layout information. 463e41f4b71Sopenharmony_ci 464e41f4b71Sopenharmony_ciThis API is supported since API version 9 and deprecated since API version 10. It is supported in ArkTS widgets. 465e41f4b71Sopenharmony_ci 466e41f4b71Sopenharmony_ci 467e41f4b71Sopenharmony_ci| Name | Type | Description | 468e41f4b71Sopenharmony_ci| ---------- | ------------------------------------------------------------ | -------------------------------------- | 469e41f4b71Sopenharmony_ci| name | string | Name of the child component. | 470e41f4b71Sopenharmony_ci| id | string | ID of the child component. | 471e41f4b71Sopenharmony_ci| constraint | [ConstraintSizeOptions](ts-types.md#constraintsizeoptions) | Constraint size of the child component. | 472e41f4b71Sopenharmony_ci| borderInfo | [LayoutBorderInfo](#layoutborderinfodeprecated) | Provides the border information of the child component. | 473e41f4b71Sopenharmony_ci| position | [Position](ts-types.md#position) | Position coordinates of the child component. | 474e41f4b71Sopenharmony_ci| measure | (childConstraint: [ConstraintSizeOptions](ts-types.md#constraintsizeoptions)) => void | Method called to apply the size constraint to the child component.| 475e41f4b71Sopenharmony_ci| layout | (LayoutInfo: [LayoutInfo](#layoutinfodeprecated)) => void | Method called to apply the specified position information to the child component.| 476e41f4b71Sopenharmony_ci 477e41f4b71Sopenharmony_ci## LayoutBorderInfo<sup>(deprecated)</sup> 478e41f4b71Sopenharmony_ci 479e41f4b71Sopenharmony_ciProvides the border information of the child component. 480e41f4b71Sopenharmony_ci 481e41f4b71Sopenharmony_ciThis API is supported since API version 9 and deprecated since API version 10. It is supported in ArkTS widgets. 482e41f4b71Sopenharmony_ci 483e41f4b71Sopenharmony_ci| Name | Type | Description | 484e41f4b71Sopenharmony_ci|-------------|--------------------------------------|-------------------------| 485e41f4b71Sopenharmony_ci| borderWidth | [EdgeWidths](ts-types.md#edgewidths9) | Edge widths in different directions of the component.| 486e41f4b71Sopenharmony_ci| margin | [Margin](ts-types.md#margin) | Margins in different directions of the component. | 487e41f4b71Sopenharmony_ci| padding | [Padding](ts-types.md#padding) | Paddings in different directions of the component. | 488e41f4b71Sopenharmony_ci 489e41f4b71Sopenharmony_ci## LayoutInfo<sup>(deprecated)</sup> 490e41f4b71Sopenharmony_ci 491e41f4b71Sopenharmony_ciProvides the layout information of the child component. 492e41f4b71Sopenharmony_ci 493e41f4b71Sopenharmony_ciThis API is supported since API version 9 and deprecated since API version 10. It is supported in ArkTS widgets. 494e41f4b71Sopenharmony_ci 495e41f4b71Sopenharmony_ci| Name | Type | Description | 496e41f4b71Sopenharmony_ci| ---------- | ---------------------------------------------------------- | ---------------- | 497e41f4b71Sopenharmony_ci| position | [Position](ts-types.md#position) | Position coordinates of the child component.| 498e41f4b71Sopenharmony_ci| constraint | [ConstraintSizeOptions](ts-types.md#constraintsizeoptions) | Constraint size of the child component.| 499e41f4b71Sopenharmony_ci 500e41f4b71Sopenharmony_ciThe layout can be modified through **layout**. 501e41f4b71Sopenharmony_ci```ts 502e41f4b71Sopenharmony_ci// xxx.ets 503e41f4b71Sopenharmony_ci@Entry 504e41f4b71Sopenharmony_ci@Component 505e41f4b71Sopenharmony_cistruct Index { 506e41f4b71Sopenharmony_ci build() { 507e41f4b71Sopenharmony_ci Column() { 508e41f4b71Sopenharmony_ci CustomLayout() { 509e41f4b71Sopenharmony_ci ForEach([1, 2, 3], (index: number) => { 510e41f4b71Sopenharmony_ci Text('Sub' + index) 511e41f4b71Sopenharmony_ci .fontSize(30) 512e41f4b71Sopenharmony_ci .borderWidth(2) 513e41f4b71Sopenharmony_ci }) 514e41f4b71Sopenharmony_ci } 515e41f4b71Sopenharmony_ci } 516e41f4b71Sopenharmony_ci } 517e41f4b71Sopenharmony_ci} 518e41f4b71Sopenharmony_ci 519e41f4b71Sopenharmony_ci 520e41f4b71Sopenharmony_ci@Component 521e41f4b71Sopenharmony_cistruct CustomLayout { 522e41f4b71Sopenharmony_ci @Builder 523e41f4b71Sopenharmony_ci doNothingBuilder() { 524e41f4b71Sopenharmony_ci }; 525e41f4b71Sopenharmony_ci 526e41f4b71Sopenharmony_ci @BuilderParam builder: () => void = this.doNothingBuilder; 527e41f4b71Sopenharmony_ci 528e41f4b71Sopenharmony_ci onLayout(children: Array<LayoutChild>, constraint: ConstraintSizeOptions) { 529e41f4b71Sopenharmony_ci let pos = 0; 530e41f4b71Sopenharmony_ci children.forEach((child) => { 531e41f4b71Sopenharmony_ci child.layout({ position: { x: pos, y: pos }, constraint: constraint }) 532e41f4b71Sopenharmony_ci pos += 70; 533e41f4b71Sopenharmony_ci }) 534e41f4b71Sopenharmony_ci } 535e41f4b71Sopenharmony_ci 536e41f4b71Sopenharmony_ci onMeasure(children: Array<LayoutChild>, constraint: ConstraintSizeOptions) { 537e41f4b71Sopenharmony_ci let size = 100; 538e41f4b71Sopenharmony_ci children.forEach((child) => { 539e41f4b71Sopenharmony_ci child.measure({ minHeight: size, minWidth: size, maxWidth: size, maxHeight: size }) 540e41f4b71Sopenharmony_ci size += 50; 541e41f4b71Sopenharmony_ci }) 542e41f4b71Sopenharmony_ci } 543e41f4b71Sopenharmony_ci 544e41f4b71Sopenharmony_ci build() { 545e41f4b71Sopenharmony_ci this.builder() 546e41f4b71Sopenharmony_ci } 547e41f4b71Sopenharmony_ci} 548e41f4b71Sopenharmony_ci``` 549e41f4b71Sopenharmony_ci 550e41f4b71Sopenharmony_ci 551