1# Responsive Grid Layout (GridRow/GridCol) 2 3 4## Overview 5 6As an auxiliary positioning tool, the responsive grid layout is handy in UI design on mobile devices. It exhibits the following advantages: 7 81. Provides rules for layout design and resolves issues of dynamic layout across devices with different sizes. By dividing a page into equal-width columns and rows, you can easily locate and typeset page elements. 9 102. Provides a unified positioning method for the system to ensure layout consistency across layouts on different devices. This can reduce the complexity of design and development and improve work efficiency. 11 123. Provides a flexible spacing adjustment method for applications to accommodate special layout requirements. You can adjust the spacing between columns and between rows to control the typesetting of the entire page. 13 144. Completes the wrapping and adaptation automatically when overflow occurs. When the number of page elements exceeds the capacity of a row or column, they automatically wrap to a new row or column and adapt the typesetting to different devices. 15 16The [\<GridRow>](../reference/arkui-ts/ts-container-gridrow.md) component is the responsive grid container component and must be used together with the [\<GridCol>](../reference/arkui-ts/ts-container-gridcol.md) child component. 17 18 19## GridRow 20 21 22### Grid Breakpoints 23 24The grid system defines breakpoints, which are screen width types in effect, based on the horizontal width (screen density pixels, in vp) of the screens. You can use the breakpoints to meet specific layout requirements. 25 26By default, the grid system provides four breakpoints: xs, sm, md, and lg. 27 28| Breakpoint| Value Range (vp) | Device Description | 29| ---- | --------------- | --------- | 30| xs | [0, 320) | Minimum-width device.| 31| sm | [320, 520) | Small-width device. | 32| md | [520, 840) | Medium-width device.| 33| lg | [840, +∞) | Large-width device. | 34 35In the **\<GridRow>** component, you can use **breakpoints** to customize the value range of breakpoints. A maximum of six breakpoints are supported. In addition to the four default breakpoints, you can also enable the xl and xxl breakpoints for your application window layout. 36 37| Breakpoint| Device Description | 38| ---- | --------- | 39| xs | Minimum-width device.| 40| sm | Small-width device. | 41| md | Medium-width device.| 42| lg | Large-width device. | 43| xl | Extra-large-width device.| 44| xxl | Extra-extra-large-width device.| 45 46- Set **breakpoints** with a monotonically increasing array based on the use case. As **breakpoints** supports a maximum of six breakpoints, the maximum length of the monotonically increasing array is 5. 47 48 49 ```ts 50 breakpoints: {value: ['100vp', '200vp']} 51 ``` 52 53 Enables three breakpoints: xs, sm, and md. If the value is less than 100 vp, the breakpoint is xs. If the value is 100–200 vp, the breakpoint is sm. If the value is greater than 200 vp, the breakpoint is md. 54 55 56 ```ts 57 breakpoints: {value: ['320vp', '520vp', '840vp', '1080vp']} 58 ``` 59 60 Enables five breakpoints: xs, sm, md, lg, and xl. If the value is less than 320 vp, the breakpoint is xs. If the value is 320–520 vp, the breakpoint is sm. If the value is 520–840 vp, the breakpoint is md. If the value is 840–1080 vp, the breakpoint is lg. If the value is greater than 1080 vp, the breakpoint is xl. 61 62- The grid system implements breakpoints by listening for the changes in the window or container size, and sets the breakpoint references through **reference**. Since the application may be displayed in non-full-screen mode, it is better to design the breakpoints with the application window width as the reference. 63 64In the following example, the default number of grid columns is 12. Breakpoints are used to divide the application window width into six ranges. In different ranges, the **\<GridCol>** child component occupies a different number of columns. 65 66 67```ts 68@State bgColors: Color[] = [Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Pink, Color.Grey, Color.Blue, Color.Brown]; 69... 70GridRow({ 71 breakpoints: { 72 value: ['200vp', '300vp', '400vp', '500vp', '600vp'], 73 reference: BreakpointsReference.WindowSize 74 } 75}) { 76 ForEach(this.bgColors, (color:Color, index?:number|undefined) => { 77 GridCol({ 78 span: { 79 xs: 2, // The <GridCol> component occupies two grid columns on the minimum-width device. 80 sm: 3, // The <GridCol> component occupies three grid columns on the small-width device. 81 md: 4, // The <GridCol> component occupies four grid columns on the medium-width device. 82 lg: 6, // The <GridCol> component occupies six grid columns on the large-width device. 83 xl: 8, // The <GridCol> component occupies eight grid columns on the extra-large-width device. 84 xxl: 12 // The <GridCol> component occupies 12 grid columns on the extra-extra-large-width device. 85 } 86 }) { 87 Row() { 88 Text(`${index}`) 89 }.width("100%").height('50vp') 90 }.backgroundColor(color) 91 }) 92} 93``` 94 95 96 97 98### Columns 99 100In the **\<GridRow>**, **columns** is used to set the total number of columns in the responsive grid layout. 101 102- The default value of **columns** is 12. If **columns** is not set, the responsive grid layout is divided into 12 columns at any breakpoint. 103 104 105 ```ts 106 @State bgColors: Color[] = [Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Pink, Color.Grey, Color.Blue, Color.Brown]; 107 ... 108 GridRow() { 109 ForEach(this.bgColors, (item:Color, index?:number|undefined) => { 110 if(index){ 111 GridCol() { 112 Row() { 113 Text(`${index + 1}`) 114 }.width('100%').height('50') 115 }.backgroundColor(item) 116 } 117 }) 118 } 119 ``` 120 121  122 123- When **columns** is set to a number, the responsive grid layout is divided into the specified number of columns regardless of the screen size. The following example sets the number of grid layout columns to 4 and 8 in sequence, where a child component occupies one column by default. 124 125 126 ```ts 127 class CurrTmp{ 128 currentBp: string = 'unknown'; 129 set(val:string){ 130 this.currentBp = val 131 } 132 } 133 let BorderWH:Record<string,Color|number> = { 'color': Color.Blue, 'width': 2 } 134 @State bgColors: Color[] = [Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Pink, Color.Grey, Color.Blue, Color.Brown]; 135 @State currentBp: string = 'unknown'; 136 ... 137 Row() { 138 GridRow({ columns: 4 }) { 139 ForEach(this.bgColors, (item:Color, index?:number|undefined) => { 140 if(index){ 141 GridCol() { 142 Row() { 143 Text(`${index.toString() + 1}`) 144 }.width('100%').height('50') 145 }.backgroundColor(item) 146 } 147 }) 148 } 149 .width('100%').height('100%') 150 .onBreakpointChange((breakpoint:string) => { 151 let CurrSet:CurrTmp = new CurrTmp() 152 CurrSet.set(breakpoint) 153 }) 154 } 155 .height(160) 156 .border(BorderWH) 157 .width('90%') 158 159 Row() { 160 GridRow({ columns: 8 }) { 161 ForEach(this.bgColors, (item:Color, index?:number|undefined) => { 162 if(index){ 163 GridCol() { 164 Row() { 165 Text(`${index.toString() + 1}`) 166 }.width('100%').height('50') 167 }.backgroundColor(item) 168 } 169 }) 170 } 171 .width('100%').height('100%') 172 .onBreakpointChange((breakpoint:string) => { 173 let CurrSet:CurrTmp = new CurrTmp() 174 CurrSet.set(breakpoint) 175 }) 176 } 177 .height(160) 178 .border(BorderWH) 179 .width('90%') 180 ``` 181 182  183 184- When **columns** is set to a value of the **GridRowColumnOption** type, you can assign values specific to the screen size (xs, sm, md, lg, xl, xxl). 185 186 187 ```ts 188 @State bgColors: Color[] = [Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Pink, Color.Grey, Color.Blue, Color.Brown] 189 GridRow({ columns: { sm: 4, md: 8 }, breakpoints: { value: ['200vp', '300vp', '400vp', '500vp', '600vp'] } }) { 190 ForEach(this.bgColors, (item:Color, index?:number|undefined) => { 191 if(index){ 192 GridCol() { 193 Row() { 194 Text(`${index.toString() + 1}`) 195 }.width('100%').height('50') 196 }.backgroundColor(item) 197 } 198 }) 199 } 200 ``` 201 202  203 204 If **columns** is only set for the sm and md screen size types, screen sizes smaller than sm use the default value **12**, and screen sizes larger than md (lg, xl, and xxl) use the value of **columns** of the md type. 205 206 207### Alignment 208 209In the responsive grid layout, you can set the **direction** attribute of **\<GridRow>** to define the direction in which child components are arranged. The options are **GridRowDirection.Row** (from left to right) or **GridRowDirection.RowReverse** (from right to left). An appropriate **direction** value can make the page layout more flexible and meet the design requirements. 210 211- When child components are arranged from left to right (default): 212 213 214 ```ts 215 GridRow({ direction: GridRowDirection.Row }){} 216 ``` 217 218  219 220- When child components are arranged from right to left (default): 221 222 223 ```ts 224 GridRow({ direction: GridRowDirection.RowReverse }){} 225 ``` 226 227  228 229 230### Gutters 231 232In the **\<GridRow>** component, **gutter** is used to set the spacing between adjacent child components in the horizontal and vertical directions. 233 234- When **gutter** is set to a number, the number applies to both the horizontal and vertical directions. In the following example, the horizontal and vertical spacing between adjacent child components is set to **10**. 235 236 237 ```ts 238 GridRow({ gutter: 10 }){} 239 ``` 240 241  242 243- When **gutter** is set to a value of the **GutterOption** type, the **x** attribute of the value indicates the horizontal gutter, and the **y** attribute indicates the vertical gutter. 244 245 246 ```ts 247 GridRow({ gutter: { x: 20, y: 50 } }){} 248 ``` 249 250  251 252 253## GridCol 254 255The **\<GridCol>** component is a child component of the **\<GridRow>** component. You can set the **span**, **offset**, and **order** attributes of this component by passing parameters or using setters. 256 257- Setting **span** 258 259 260 ```ts 261 let Gspan:Record<string,number> = { 'xs': 1, 'sm': 2, 'md': 3, 'lg': 4 } 262 GridCol({ span: 2 }){} 263 GridCol({ span: { xs: 1, sm: 2, md: 3, lg: 4 } }){} 264 GridCol(){}.span(2) 265 GridCol(){}.span(Gspan) 266 ``` 267 268- Setting **offset** 269 270 271 ```ts 272 let Goffset:Record<string,number> = { 'xs': 1, 'sm': 2, 'md': 3, 'lg': 4 } 273 GridCol({ offset: 2 }){} 274 GridCol({ offset: { xs: 2, sm: 2, md: 2, lg: 2 } }){} 275 GridCol(){}.offset(2) 276 GridCol(){}.offset(Goffset) 277 ``` 278 279- Setting **order** 280 281 282 ```ts 283 let Gorder:Record<string,number> = { 'xs': 1, 'sm': 2, 'md': 3, 'lg': 4 } 284 GridCol({ order: 2 }){} 285 GridCol({ order: { xs: 1, sm: 2, md: 3, lg: 4 } }){} 286 GridCol(){}.order(2) 287 GridCol(){}.order(Gorder) 288 ``` 289 290 291### span 292 293Sets the number of columns occupied by a child component in the grid layout, which determines the child component width. The default value is **1**. 294 295- When the value type is number, the number of columns occupied by the child component is the same across screen sizes. 296 297 298 ```ts 299 @State bgColors: Color[] = [Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Pink, Color.Grey, Color.Blue, Color.Brown]; 300 ... 301 GridRow({ columns: 8 }) { 302 ForEach(this.bgColors, (color:Color, index?:number|undefined) => { 303 GridCol({ span: 2 }) { 304 Row() { 305 Text(`${index}`) 306 }.width('100%').height('50vp') 307 } 308 .backgroundColor(color) 309 }) 310 } 311 ``` 312 313  314 315- When the value type is **GridColColumnOption**, you can assign values specific to the screen size (xs, sm, md, lg, xl, xxl). 316 317 318 ```ts 319 @State bgColors: Color[] = [Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Pink, Color.Grey, Color.Blue, Color.Brown]; 320 ... 321 GridRow({ columns: 8 }) { 322 ForEach(this.bgColors, (color:Color, index?:number|undefined) => { 323 GridCol({ span: { xs: 1, sm: 2, md: 3, lg: 4 } }) { 324 Row() { 325 Text(`${index}`) 326 }.width('100%').height('50vp') 327 } 328 .backgroundColor(color) 329 }) 330 } 331 ``` 332 333  334 335 336### offset 337 338Sets the column offset of a child component relative to the previous child component. The default value is **0**. 339 340- When the value type is number, the column offset of the child component is the same across screen sizes. 341 342 343 ```ts 344 @State bgColors: Color[] = [Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Pink, Color.Grey, Color.Blue, Color.Brown]; 345 ... 346 GridRow() { 347 ForEach(this.bgColors, (color:Color, index?:number|undefined) => { 348 GridCol({ offset: 2 }) { 349 Row() { 350 Text('' + index) 351 }.width('100%').height('50vp') 352 } 353 .backgroundColor(color) 354 }) 355 } 356 ``` 357 358  359 360 By default, a grid is divided into 12 columns and each child component occupies one column with an offset of two columns. Each row holds four child components, with three columns per child component plus the gutter. 361 362- When the value type is **GridColColumnOption**, you can assign values specific to the screen size (xs, sm, md, lg, xl, xxl). 363 364 365 ```ts 366 @State bgColors: Color[] = [Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Pink, Color.Grey, Color.Blue, Color.Brown]; 367 ... 368 369 GridRow() { 370 ForEach(this.bgColors, (color:Color, index?:number|undefined) => { 371 GridCol({ offset: { xs: 1, sm: 2, md: 3, lg: 4 } }) { 372 Row() { 373 Text('' + index) 374 }.width('100%').height('50vp') 375 } 376 .backgroundColor(color) 377 }) 378 } 379 ``` 380 381  382 383 384### order 385 386Sets the sequence number of a child component in the grid layout. If a child component shares an **order** value with another child component or does not have **order** set, it is displayed based on its code sequence number. A child component with a smaller **order** value is placed before the one with a larger **order** value. 387 388If **order** is not set for all child components, those that have **order** set are displayed after those that do not have **order** set and are sorted in ascending order based on the value. 389 390- When the value type is number, child components are sorted in the same order across screen sizes. 391 392 393 ```ts 394 GridRow() { 395 GridCol({ order: 4 }) { 396 Row() { 397 Text('1') 398 }.width('100%').height('50vp') 399 }.backgroundColor(Color.Red) 400 GridCol({ order: 3 }) { 401 Row() { 402 Text('2') 403 }.width('100%').height('50vp') 404 }.backgroundColor(Color.Orange) 405 GridCol({ order: 2 }) { 406 Row() { 407 Text('3') 408 }.width('100%').height('50vp') 409 }.backgroundColor(Color.Yellow) 410 GridCol({ order: 1 }) { 411 Row() { 412 Text('4') 413 }.width('100%').height('50vp') 414 }.backgroundColor(Color.Green) 415 } 416 ``` 417 418  419 420- When the value type is **GridColColumnOption**, you can assign values specific to the screen size (xs, sm, md, lg, xl, xxl). You can set 1234 for xs, 2341 for sm, 3412 for md, and 2431 for lg. 421 422 423 ```ts 424 GridRow() { 425 GridCol({ order: { xs:1, sm:5, md:3, lg:7}}) { 426 Row() { 427 Text('1') 428 }.width('100%').height('50vp') 429 }.backgroundColor(Color.Red) 430 GridCol({ order: { xs:2, sm:2, md:6, lg:1} }) { 431 Row() { 432 Text('2') 433 }.width('100%').height('50vp') 434 }.backgroundColor(Color.Orange) 435 GridCol({ order: { xs:3, sm:3, md:1, lg:6} }) { 436 Row() { 437 Text('3') 438 }.width('100%').height('50vp') 439 }.backgroundColor(Color.Yellow) 440 GridCol({ order: { xs:4, sm:4, md:2, lg:5} }) { 441 Row() { 442 Text('4') 443 }.width('100%').height('50vp') 444 }.backgroundColor(Color.Green) 445 } 446 ``` 447 448  449 450 451## Nesting of Responsive Grid Components 452 453Responsive grid components can be contained in other responsive grid components. 454 455In the following example, the responsive grid divides the entire space into 12 parts. At the first layer, **\<GridCol>** is nested in **\<GridRow>**, and the space is divided into the large area in the center and the footer area. At the second layer, **\<GridCol>** is nested in **\<GridRow>**, and the space is divided into the left and right areas. The child components take up the space allocated by the parent component at the upper layer. In this example, the pink area is made up of 12 columns of the screen space, and the green and blue areas take up the 12 columns of the parent component proportionally. 456 457 458 459```ts 460@Entry 461@Component 462struct GridRowExample { 463 build() { 464 GridRow() { 465 GridCol({ span: { sm: 12 } }) { 466 GridRow() { 467 GridCol({ span: { sm: 2 } }) { 468 Row() { 469 Text('left').fontSize(24) 470 } 471 .justifyContent(FlexAlign.Center) 472 .height('90%') 473 }.backgroundColor('#ff41dbaa') 474 475 GridCol({ span: { sm: 10 } }) { 476 Row() { 477 Text('right').fontSize(24) 478 } 479 .justifyContent(FlexAlign.Center) 480 .height('90%') 481 }.backgroundColor('#ff4168db') 482 } 483 .backgroundColor('#19000000') 484 .height('100%') 485 } 486 487 GridCol({ span: { sm: 12 } }) { 488 Row() { 489 Text('footer').width('100%').textAlign(TextAlign.Center) 490 }.width('100%').height('10%').backgroundColor(Color.Pink) 491 } 492 }.width('100%').height(300) 493 } 494} 495``` 496 497 498 499 500 501To sum up, the responsive grid components are powerful tools with a wide range of customization capabilities. With the required attributes set at different breakpoints, such as **Columns**, **Margin**, **Gutter**, and **span**, the layout is created automatically. You do not need to pay attention to the specific device type and device state (such as landscape and portrait). 502