1# Layout Constraints 2 3Layout constraints refer to constraints on the aspect ratio and display priority of components. 4 5> **NOTE** 6> 7> The APIs of this module are supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version. 8 9## aspectRatio 10 11aspectRatio(value: number) 12 13Sets the aspect ratio of the component. 14 15**Widget capability**: This API can be used in ArkTS widgets since API version 9. 16 17**Atomic service API**: This API can be used in atomic services since API version 11. 18 19**System capability**: SystemCapability.ArkUI.ArkUI.Full 20 21**Parameters** 22 23| Name| Type | Mandatory| Description | 24| ------ | ------ | ---- | ------------------------------------------------------------ | 25| value | number | Yes | Aspect ratio of the component, which can be obtained using the following formula: Width/Height.<br>The default value varies by API version.<br>API version 9 and earlier: **1.0**<br><br>API version 10: none<br>**NOTE**<br>This attribute does not take effect when it is not set or is set to an invalid value.<br>For example, if a **\<Row>** component has only its width set and does not have any child component, then when **aspectRatio** is not set or is set to a negative value, the height of the **\<Row>** component is 0.| 26 27## displayPriority 28 29displayPriority(value: number) 30 31Sets the display priority for the component in the layout container. 32 33**Widget capability**: This API can be used in ArkTS widgets since API version 9. 34 35**Atomic service API**: This API can be used in atomic services since API version 11. 36 37**System capability**: SystemCapability.ArkUI.ArkUI.Full 38 39**Parameters** 40 41| Name| Type | Mandatory| Description | 42| ------ | ------ | ---- | ------------------------------------------------------------ | 43| value | number | Yes | Display priority of the component in the layout container.<br>Default value: **1**<br>**NOTE**<br>This parameter is only effective in [Row](./ts-container-row.md), [Column](./ts-container-column.md), and [Flex (single-line)](./ts-container-flex.md) container components.<br> The digits after the decimal point are not counted in determining the display priority. That is, numbers in the [x, x + 1) range are considered to represent the same priority. For example, **1.0** and **1.9** represent the same priority.<br>If the **displayPriority** value of all child components is not greater than 1, there is no difference in priority.<br>When the **displayPriority** value of a child component is greater than 1, a larger value indicates higher priority. If the parent container does not have enough space, child components with lower priority are hidden. If child components of a certain priority are hidden, those with an even lower priority are also hidden.| 44 45## pixelRound<sup>11+</sup> 46 47pixelRound(value: PixelRoundPolicy) 48 49Describes the rounding strategy for component pixel-level alignment. 50 51**Widget capability**: This API can be used in ArkTS widgets since API version 11. 52 53**Atomic service API**: This API can be used in atomic services since API version 11. 54 55**System capability**: SystemCapability.ArkUI.ArkUI.Full 56 57**Parameters** 58 59| Name| Type | Mandatory| Description | 60| ------ | ------ | ---- | ------------------------------------------------------------ | 61| value | [PixelRoundPolicy](ts-types.md#pixelroundpolicy11) | Yes| Rounding strategy for component pixel-level alignment.<br>**NOTE**<br>This attribute is applicable in scenarios where artifacts occur due to floating-point drawing.| 62 63## Example 64 65### Example 1 66 67```ts 68// xxx.ets 69@Entry 70@Component 71struct AspectRatioExample { 72 private children: string[] = ['1', '2', '3', '4', '5', '6'] 73 74 build() { 75 Column({ space: 20 }) { 76 Text('using container: row').fontSize(14).fontColor(0xCCCCCC).width('100%') 77 Row({ space: 10 }) { 78 ForEach(this.children, (item:string) => { 79 // Component width = Component height x 1.5 = 90 80 Text(item) 81 .backgroundColor(0xbbb2cb) 82 .fontSize(20) 83 .aspectRatio(1.5) 84 .height(60) 85 // Component height = Component width/1.5 = 60/1.5 = 40 86 Text(item) 87 .backgroundColor(0xbbb2cb) 88 .fontSize(20) 89 .aspectRatio(1.5) 90 .width(60) 91 }, (item:string) => item) 92 } 93 .size({ width: "100%", height: 100 }) 94 .backgroundColor(0xd2cab3) 95 .clip(true) 96 97 // Grid child component width/height = 3/2 98 Text('using container: grid').fontSize(14).fontColor(0xCCCCCC).width('100%') 99 Grid() { 100 ForEach(this.children, (item:string) => { 101 GridItem() { 102 Text(item) 103 .backgroundColor(0xbbb2cb) 104 .fontSize(40) 105 .width('100%') 106 .aspectRatio(1.5) 107 } 108 }, (item:string) => item) 109 } 110 .columnsTemplate('1fr 1fr 1fr') 111 .columnsGap(10) 112 .rowsGap(10) 113 .size({ width: "100%", height: 165 }) 114 .backgroundColor(0xd2cab3) 115 }.padding(10) 116 } 117} 118``` 119 120**Figure 1** Portrait display<br> 121 122 123**Figure 2** Landscape display<br> 124 125 126### Example 2 127 128```ts 129class ContainerInfo { 130 label: string = ''; 131 size: string = ''; 132} 133 134class ChildInfo { 135 text: string = ''; 136 priority: number = 0; 137} 138 139@Entry 140@Component 141struct DisplayPriorityExample { 142 // Display the container size. 143 private container: ContainerInfo[] = [ 144 { label: 'Big container', size: '90%' }, 145 { label: 'Middle container', size: '50%' }, 146 { label: 'Small container', size: '30%' } 147 ] 148 private children: ChildInfo[] = [ 149 { text: '1\n(priority:2)', priority: 2 }, 150 { text: '2\n(priority:1)', priority: 1 }, 151 { text: '3\n(priority:3)', priority: 3 }, 152 { text: '4\n(priority:1)', priority: 1 }, 153 { text: '5\n(priority:2)', priority: 2 } 154 ] 155 @State currentIndex: number = 0; 156 157 build() { 158 Column({ space: 10 }) { 159 // Switch the size of the parent container. 160 Button(this.container[this.currentIndex].label).backgroundColor(0x317aff) 161 .onClick(() => { 162 this.currentIndex = (this.currentIndex + 1) % this.container.length; 163 }) 164 // Set the width for the parent flex container through variables. 165 Flex({ justifyContent: FlexAlign.SpaceBetween }) { 166 ForEach(this.children, (item:ChildInfo) => { 167 // Bind the display priority to the child component through displayPriority. 168 Text(item.text) 169 .width(120) 170 .height(60) 171 .fontSize(24) 172 .textAlign(TextAlign.Center) 173 .backgroundColor(0xbbb2cb) 174 .displayPriority(item.priority) 175 }, (item:ChildInfo) => item.text) 176 } 177 .width(this.container[this.currentIndex].size) 178 .backgroundColor(0xd2cab3) 179 }.width("100%").margin({ top: 50 }) 180 } 181} 182``` 183 184Landscape display in containers of different sizes 185 186 187 188### Example 3 189 190Example of using **pixelRound**: 191 192```ts 193@Entry 194@Component 195struct PixelRoundExample { 196 build() { 197 Column() { 198 Row() { 199 Row() { 200 } 201 .width('100%') 202 .height('100%') 203 .backgroundColor(Color.Yellow) 204 } 205 .width('300.6px') 206 .height('300.6px') 207 .backgroundColor(Color.Red) 208 .position({x: '200.2px', y: '100.2px'}) 209 .pixelRound({ 210 start:PixelRoundCalcPolicy.FORCE_CEIL, 211 top:PixelRoundCalcPolicy.FORCE_CEIL 212 }) 213 } 214 .width("100%") 215 .height('100%') 216 .backgroundColor('#ffe5e5e5') 217 } 218} 219``` 220**Figure 1** Layout with pixelRound 221 222 223 224**Figure 2** Layout without pixelRound 225 226 227