1# Linear Layout (Row/Column) 2 3 4## Overview 5 6Linear layout is the most frequently used layout in development, built with the [Row](../reference/apis-arkui/arkui-ts/ts-container-row.md) or [Column](../reference/apis-arkui/arkui-ts/ts-container-column.md) linear containers. The linear layout is the basis of other layouts. Its child elements are arranged in sequence linearly in the horizontal direction, as in a **Row** container, or vertical direction, as in a **Column** container. 7 8 9 **Figure 1** Child element arrangement in a Column container 10 11 12 13 14 **Figure 2** Child element arrangement in a Row container 15 16 17 18 19## Basic Concepts 20 21- Layout container: container component that is able to lay out other elements as its child elements. The layout container calculates the size of its child elements and arranges the layout. 22 23- Layout child element: element inside the layout container. 24 25- Main axis: axis along which child elements are laid out by default in the linear layout container. The main axis is horizontal for the **Row** container and vertical for the **Column** container. 26 27- Cross axis: axis that runs perpendicular to the main axis. The cross axis is vertical for the **Row** container and horizontal for the **Column** container. 28 29- Spacing: distance between child elements. 30 31 32## Spacing of Child Elements in Arrangement Direction 33 34In the layout container, use the **space** attribute to equally space child elements in the arrangement direction. 35 36 37### In Column Container 38 39 **Figure 3** Layout child element spacing in the arrangement direction in the Column container 40 41 42 43```ts 44Column({ space: 20 }) { 45 Text('space: 20').fontSize(15).fontColor(Color.Gray).width('90%') 46 Row().width('90%').height(50).backgroundColor(0xF5DEB3) 47 Row().width('90%').height(50).backgroundColor(0xD2B48C) 48 Row().width('90%').height(50).backgroundColor(0xF5DEB3) 49}.width('100%') 50``` 51 52 53 54 55 56### In Row Container 57 58 **Figure 4** Layout child element spacing in the arrangement direction in the Row container 59 60 61 62 63```ts 64Row({ space: 35 }) { 65 Text('space: 35').fontSize(15).fontColor(Color.Gray) 66 Row().width('10%').height(150).backgroundColor(0xF5DEB3) 67 Row().width('10%').height(150).backgroundColor(0xD2B48C) 68 Row().width('10%').height(150).backgroundColor(0xF5DEB3) 69}.width('90%') 70``` 71 72 73 74 75## Alignment of Child Elements Along Cross Axis 76 77In the layout container, use the **alignItems** attribute to set the alignment mode of child elements along the cross axis. The alignment performance is consistent across screens of various sizes. The value is of the [VerticalAlign Type](../reference/apis-arkui/arkui-ts/ts-appendix-enums.md#verticalalign) type when the cross axis is in the vertical direction and the [HorizontalAlign](../reference/apis-arkui/arkui-ts/ts-appendix-enums.md#horizontalalign) type when the cross axis is in the horizontal direction. 78 79The layout container also provides the **alignSelf** attribute to control the alignment mode of a single child element along the cross axis. This attribute has a higher priority than the **alignItems** attribute. This means that, if **alignSelf** is set, it will overwrite the **alignItems** setting on the corresponding child element. 80 81 82### Horizontal Alignment of Child Elements in Column Container 83 84 **Figure 5** Horizontal alignment of child elements in the Column container 85 86 87 88- **HorizontalAlign.Start**: Child elements are left aligned horizontally. 89 90 ```ts 91 Column({}) { 92 Column() { 93 }.width('80%').height(50).backgroundColor(0xF5DEB3) 94 95 Column() { 96 }.width('80%').height(50).backgroundColor(0xD2B48C) 97 98 Column() { 99 }.width('80%').height(50).backgroundColor(0xF5DEB3) 100 }.width('100%').alignItems(HorizontalAlign.Start).backgroundColor('rgb(242,242,242)') 101 ``` 102 103  104 105- **HorizontalAlign.Center**: Child elements are center-aligned horizontally. 106 107 ```ts 108 Column({}) { 109 Column() { 110 }.width('80%').height(50).backgroundColor(0xF5DEB3) 111 112 Column() { 113 }.width('80%').height(50).backgroundColor(0xD2B48C) 114 115 Column() { 116 }.width('80%').height(50).backgroundColor(0xF5DEB3) 117 }.width('100%').alignItems(HorizontalAlign.Center).backgroundColor('rgb(242,242,242)') 118 ``` 119 120  121 122- **HorizontalAlign.End**: Child elements are right-aligned horizontally. 123 124 ```ts 125 Column({}) { 126 Column() { 127 }.width('80%').height(50).backgroundColor(0xF5DEB3) 128 129 Column() { 130 }.width('80%').height(50).backgroundColor(0xD2B48C) 131 132 Column() { 133 }.width('80%').height(50).backgroundColor(0xF5DEB3) 134 }.width('100%').alignItems(HorizontalAlign.End).backgroundColor('rgb(242,242,242)') 135 ``` 136 137  138 139 140### Vertical Alignment of Child Elements in Row Container 141 142 **Figure 6** Vertical alignment of child elements in Row container 143 144 145 146- **VerticalAlign.Top**: Child elements are top-aligned vertically. 147 148 ```ts 149 Row({}) { 150 Column() { 151 }.width('20%').height(30).backgroundColor(0xF5DEB3) 152 153 Column() { 154 }.width('20%').height(30).backgroundColor(0xD2B48C) 155 156 Column() { 157 }.width('20%').height(30).backgroundColor(0xF5DEB3) 158 }.width('100%').height(200).alignItems(VerticalAlign.Top).backgroundColor('rgb(242,242,242)') 159 ``` 160 161  162 163- **VerticalAlign.Center**: Child elements are center-aligned vertically. 164 165 ```ts 166 Row({}) { 167 Column() { 168 }.width('20%').height(30).backgroundColor(0xF5DEB3) 169 170 Column() { 171 }.width('20%').height(30).backgroundColor(0xD2B48C) 172 173 Column() { 174 }.width('20%').height(30).backgroundColor(0xF5DEB3) 175 }.width('100%').height(200).alignItems(VerticalAlign.Center).backgroundColor('rgb(242,242,242)') 176 ``` 177 178  179 180- **VerticalAlign.Bottom**: Child elements are bottom-aligned vertically. 181 182 ```ts 183 Row({}) { 184 Column() { 185 }.width('20%').height(30).backgroundColor(0xF5DEB3) 186 187 Column() { 188 }.width('20%').height(30).backgroundColor(0xD2B48C) 189 190 Column() { 191 }.width('20%').height(30).backgroundColor(0xF5DEB3) 192 }.width('100%').height(200).alignItems(VerticalAlign.Bottom).backgroundColor('rgb(242,242,242)') 193 ``` 194 195  196 197 198## Arrangement of Child Elements Along Main Axis 199 200In the layout container, you can use the **justifyContent** attribute to set the arrangement mode of child elements along the main axis. The arrangement may begin from the start point or end point of the main axis, or the space of the main axis can be evenly divided. 201 202 203### Vertical Alignment of Child Elements in Column Container 204 205 **Figure 7** Vertical alignment of child elements in the Column container 206 207 208 209- **justifyContent(FlexAlign.Start)**: The items are vertically aligned with each other toward the start edge of the container. 210 211 ```ts 212 Column({}) { 213 Column() { 214 }.width('80%').height(50).backgroundColor(0xF5DEB3) 215 216 Column() { 217 }.width('80%').height(50).backgroundColor(0xD2B48C) 218 219 Column() { 220 }.width('80%').height(50).backgroundColor(0xF5DEB3) 221 }.width('100%').height(300).backgroundColor('rgb(242,242,242)').justifyContent(FlexAlign.Start) 222 ``` 223 224  225 226- **justifyContent(FlexAlign.Center)**: The elements are vertically aligned with each other toward the center of the container. The space between the first component and the start edge is the same as that between the last component and the end edge. 227 228 ```ts 229 Column({}) { 230 Column() { 231 }.width('80%').height(50).backgroundColor(0xF5DEB3) 232 233 Column() { 234 }.width('80%').height(50).backgroundColor(0xD2B48C) 235 236 Column() { 237 }.width('80%').height(50).backgroundColor(0xF5DEB3) 238 }.width('100%').height(300).backgroundColor('rgb(242,242,242)').justifyContent(FlexAlign.Center) 239 ``` 240 241  242 243- **justifyContent(FlexAlign.End)**: The elements are vertically aligned with each other toward the end edge of the container. 244 245 ```ts 246 Column({}) { 247 Column() { 248 }.width('80%').height(50).backgroundColor(0xF5DEB3) 249 250 Column() { 251 }.width('80%').height(50).backgroundColor(0xD2B48C) 252 253 Column() { 254 }.width('80%').height(50).backgroundColor(0xF5DEB3) 255 }.width('100%').height(300).backgroundColor('rgb(242,242,242)').justifyContent(FlexAlign.End) 256 ``` 257 258  259 260- **justifyContent(FlexAlign.SpaceBetween)**: The elements are evenly distributed vertically. The space between any two adjacent elements is the same. The first element is aligned with the start edge, the last element is aligned with the end edge, and the remaining elements are distributed so that the space between any two adjacent elements is the same. 261 262 ```ts 263 Column({}) { 264 Column() { 265 }.width('80%').height(50).backgroundColor(0xF5DEB3) 266 267 Column() { 268 }.width('80%').height(50).backgroundColor(0xD2B48C) 269 270 Column() { 271 }.width('80%').height(50).backgroundColor(0xF5DEB3) 272 }.width('100%').height(300).backgroundColor('rgb(242,242,242)').justifyContent(FlexAlign.SpaceBetween) 273 ``` 274 275  276 277- **justifyContent(FlexAlign.SpaceAround)**: The elements are evenly distributed vertically. The space between any two adjacent elements is the same. The space between the first element and start edge, and that between the last element and end edge are both half the size of the space between two adjacent elements. 278 279 ```ts 280 Column({}) { 281 Column() { 282 }.width('80%').height(50).backgroundColor(0xF5DEB3) 283 284 Column() { 285 }.width('80%').height(50).backgroundColor(0xD2B48C) 286 287 Column() { 288 }.width('80%').height(50).backgroundColor(0xF5DEB3) 289 }.width('100%').height(300).backgroundColor('rgb(242,242,242)').justifyContent(FlexAlign.SpaceAround) 290 ``` 291 292  293 294- **justifyContent(FlexAlign.SpaceEvenly)**: The elements are evenly distributed vertically. The space between the first element and start edge, the space between the last element and end edge, and the space between any two adjacent elements are the same. 295 296 ```ts 297 Column({}) { 298 Column() { 299 }.width('80%').height(50).backgroundColor(0xF5DEB3) 300 301 Column() { 302 }.width('80%').height(50).backgroundColor(0xD2B48C) 303 304 Column() { 305 }.width('80%').height(50).backgroundColor(0xF5DEB3) 306 }.width('100%').height(300).backgroundColor('rgb(242,242,242)').justifyContent(FlexAlign.SpaceEvenly) 307 ``` 308 309  310 311 312### Horizontal Alignment of Child Elements in Row Container 313 314 **Figure 8** Horizontal alignment of child elements in the Row container 315 316 317 318- **justifyContent(FlexAlign.Start)**: The items are horizontally aligned with each other toward the start edge of the container. 319 320 ```ts 321 Row({}) { 322 Column() { 323 }.width('20%').height(30).backgroundColor(0xF5DEB3) 324 325 Column() { 326 }.width('20%').height(30).backgroundColor(0xD2B48C) 327 328 Column() { 329 }.width('20%').height(30).backgroundColor(0xF5DEB3) 330 }.width('100%').height(200).backgroundColor('rgb(242,242,242)').justifyContent(FlexAlign.Start) 331 ``` 332 333  334 335- **justifyContent(FlexAlign.Center)**: The elements are horizontally aligned with each other toward the center of the container. The space between the first component and the start edge is the same as that between the last component and the end edge. 336 337 ```ts 338 Row({}) { 339 Column() { 340 }.width('20%').height(30).backgroundColor(0xF5DEB3) 341 342 Column() { 343 }.width('20%').height(30).backgroundColor(0xD2B48C) 344 345 Column() { 346 }.width('20%').height(30).backgroundColor(0xF5DEB3) 347 }.width('100%').height(200).backgroundColor('rgb(242,242,242)').justifyContent(FlexAlign.Center) 348 ``` 349 350  351 352- **justifyContent(FlexAlign.End)**: The elements are horizontally aligned with each other toward the end edge of the container. 353 354 ```ts 355 Row({}) { 356 Column() { 357 }.width('20%').height(30).backgroundColor(0xF5DEB3) 358 359 Column() { 360 }.width('20%').height(30).backgroundColor(0xD2B48C) 361 362 Column() { 363 }.width('20%').height(30).backgroundColor(0xF5DEB3) 364 }.width('100%').height(200).backgroundColor('rgb(242,242,242)').justifyContent(FlexAlign.End) 365 ``` 366 367  368 369- **justifyContent(FlexAlign.SpaceBetween)**: The elements are evenly distributed horizontally. The space between any two adjacent elements is the same. The first element is aligned with the start edge, the last element is aligned with the end edge, and the remaining elements are distributed so that the space between any two adjacent elements is the same. 370 371 ```ts 372 Row({}) { 373 Column() { 374 }.width('20%').height(30).backgroundColor(0xF5DEB3) 375 376 Column() { 377 }.width('20%').height(30).backgroundColor(0xD2B48C) 378 379 Column() { 380 }.width('20%').height(30).backgroundColor(0xF5DEB3) 381 }.width('100%').height(200).backgroundColor('rgb(242,242,242)').justifyContent(FlexAlign.SpaceBetween) 382 ``` 383 384  385 386- **justifyContent(FlexAlign.SpaceAround)**: The elements are evenly distributed horizontally. The space between any two adjacent elements is the same. The space between the first element and start edge, and that between the last element and end edge are both half the size of the space between two adjacent elements. 387 388 ```ts 389 Row({}) { 390 Column() { 391 }.width('20%').height(30).backgroundColor(0xF5DEB3) 392 393 Column() { 394 }.width('20%').height(30).backgroundColor(0xD2B48C) 395 396 Column() { 397 }.width('20%').height(30).backgroundColor(0xF5DEB3) 398 }.width('100%').height(200).backgroundColor('rgb(242,242,242)').justifyContent(FlexAlign.SpaceAround) 399 ``` 400 401  402 403- **justifyContent(FlexAlign.SpaceEvenly)**: The elements are evenly distributed horizontally. The space between the first element and start edge, the space between the last element and end edge, and the space between any two adjacent elements are the same. 404 405 ```ts 406 Row({}) { 407 Column() { 408 }.width('20%').height(30).backgroundColor(0xF5DEB3) 409 410 Column() { 411 }.width('20%').height(30).backgroundColor(0xD2B48C) 412 413 Column() { 414 }.width('20%').height(30).backgroundColor(0xF5DEB3) 415 }.width('100%').height(200).backgroundColor('rgb(242,242,242)').justifyContent(FlexAlign.SpaceEvenly) 416 ``` 417 418  419 420 421## Adaptive Stretching 422 423In linear layout, adaptive stretching is achieved by using the [Blank](../reference/apis-arkui/arkui-ts/ts-basic-components-blank.md) component, which automatically fills the empty spaces in the container – **Row** or **Column** – along the main axis. Just add the width and height as a percentage, and then adaptive scaling will take effect once the screen width and height change. 424 425 426```ts 427@Entry 428@Component 429struct BlankExample { 430 build() { 431 Column() { 432 Row() { 433 Text('Bluetooth').fontSize(18) 434 Blank() 435 Toggle({ type: ToggleType.Switch, isOn: true }) 436 }.backgroundColor(0xFFFFFF).borderRadius(15).padding({ left: 12 }).width('100%') 437 }.backgroundColor(0xEFEFEF).padding(20).width('100%') 438 } 439} 440``` 441 442 **Figure 9** Portrait mode 443 444 445 446 **Figure 10** Landscape mode 447 448 449 450 451## Adaptive Scaling 452 453Adaptive scaling means that the size of a child element is automatically adjusted according to a preset ratio to fit into the container across devices of various screen sizes. In linear layout, adaptive scaling can be achieved using either of the following methods: 454 455 456- When the container size is determined, use **layoutWeight** to set the weight of a child element during layout. The container space is then allocated along the main axis among the element and sibling elements based on the set layout weight, ignoring the size settings of the elements themselves. 457 458 ```ts 459 @Entry 460 @Component 461 struct layoutWeightExample { 462 build() { 463 Column() { 464 Text('1:2:3').width('100%') 465 Row() { 466 Column() { 467 Text('layoutWeight(1)') 468 .textAlign(TextAlign.Center) 469 }.layoutWeight(1).backgroundColor(0xF5DEB3).height('100%') 470 471 Column() { 472 Text('layoutWeight(2)') 473 .textAlign(TextAlign.Center) 474 }.layoutWeight(2).backgroundColor(0xD2B48C).height('100%') 475 476 Column() { 477 Text('layoutWeight(3)') 478 .textAlign(TextAlign.Center) 479 }.layoutWeight(3).backgroundColor(0xF5DEB3).height('100%') 480 481 }.backgroundColor(0xffd306).height('30%') 482 483 Text('2:5:3').width('100%') 484 Row() { 485 Column() { 486 Text('layoutWeight(2)') 487 .textAlign(TextAlign.Center) 488 }.layoutWeight(2).backgroundColor(0xF5DEB3).height('100%') 489 490 Column() { 491 Text('layoutWeight(5)') 492 .textAlign(TextAlign.Center) 493 }.layoutWeight(5).backgroundColor(0xD2B48C).height('100%') 494 495 Column() { 496 Text('layoutWeight(3)') 497 .textAlign(TextAlign.Center) 498 }.layoutWeight(3).backgroundColor(0xF5DEB3).height('100%') 499 }.backgroundColor(0xffd306).height('30%') 500 } 501 } 502 } 503 ``` 504 505 **Figure 11** Landscape mode 506 507  508 509 **Figure 12** Portrait mode 510 511  512 513- When the container size is determined, set the width of a child element in percentage. The container space is then allocated among the element and sibling elements based on the set percentage. 514 515 ```ts 516 @Entry 517 @Component 518 struct WidthExample { 519 build() { 520 Column() { 521 Row() { 522 Column() { 523 Text('left width 20%') 524 .textAlign(TextAlign.Center) 525 }.width('20%').backgroundColor(0xF5DEB3).height('100%') 526 527 Column() { 528 Text('center width 50%') 529 .textAlign(TextAlign.Center) 530 }.width('50%').backgroundColor(0xD2B48C).height('100%') 531 532 Column() { 533 Text('right width 30%') 534 .textAlign(TextAlign.Center) 535 }.width('30%').backgroundColor(0xF5DEB3).height('100%') 536 }.backgroundColor(0xffd306).height('30%') 537 } 538 } 539 } 540 ``` 541 542 **Figure 13** Landscape mode 543 544  545 546 **Figure 14** Portrait mode 547 548  549 550 551## Adaptive Extension 552 553Adaptive extension allows users to drag the scrollbar to display the page content outside the screen. It is applicable to the scenario where the content extends beyond the viewport in linear layout. Below are the methods to implement adaptive extension in linear layout: 554 555- [Add a scrollbar to a List component](arkts-layout-development-create-list.md#adding-a-scrollbar): If the list items cannot be fully displayed on one screen, you can place the child elements in different components and employ a scrollbar to display them. Use the **scrollBar** attribute to set the scrollbar status and the **edgeEffect** attribute to set the rebound effect when the scrollbar has reached the edge. 556 557- Use a **Scroll** component: When one screen is not able to accommodate the full content, you can wrap a **Scroll** component at the outer layer of the **Column** or **Row** component to implement a scrollable linear layout. 558 Example of using a **Scroll** component in the vertical layout: 559 560 ```ts 561 @Entry 562 @Component 563 struct ScrollExample { 564 scroller: Scroller = new Scroller(); 565 private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; 566 567 build() { 568 Scroll(this.scroller) { 569 Column() { 570 ForEach(this.arr, (item?:number|undefined) => { 571 if(item){ 572 Text(item.toString()) 573 .width('90%') 574 .height(150) 575 .backgroundColor(0xFFFFFF) 576 .borderRadius(15) 577 .fontSize(16) 578 .textAlign(TextAlign.Center) 579 .margin({ top: 10 }) 580 } 581 }, (item:number) => item.toString()) 582 }.width('100%') 583 } 584 .backgroundColor(0xDCDCDC) 585 .scrollable(ScrollDirection.Vertical) // Vertical scrolling. 586 .scrollBar(BarState.On) // The scrollbar is always displayed. 587 .scrollBarColor(Color.Gray) // The scrollbar color is gray. 588 .scrollBarWidth(10) // The scrollbar width is 10. 589 .edgeEffect(EdgeEffect.Spring) // The spring effect is produced when the scrollbar has reached the edge. 590 } 591 } 592 ``` 593 594  595 596 Example of using a **Scroll** component in the horizontal layout: 597 598 599 ```ts 600 @Entry 601 @Component 602 struct ScrollExample { 603 scroller: Scroller = new Scroller(); 604 private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; 605 606 build() { 607 Scroll(this.scroller) { 608 Row() { 609 ForEach(this.arr, (item?:number|undefined) => { 610 if(item){ 611 Text(item.toString()) 612 .height('90%') 613 .width(150) 614 .backgroundColor(0xFFFFFF) 615 .borderRadius(15) 616 .fontSize(16) 617 .textAlign(TextAlign.Center) 618 .margin({ left: 10 }) 619 } 620 }) 621 }.height('100%') 622 } 623 .backgroundColor(0xDCDCDC) 624 .scrollable(ScrollDirection.Horizontal) // Horizontal scrolling. 625 .scrollBar(BarState.On) // The scrollbar is always displayed. 626 .scrollBarColor(Color.Gray) // The scrollbar color is gray. 627 .scrollBarWidth(10) // The scrollbar width is 10. 628 .edgeEffect(EdgeEffect.Spring) // The spring effect is produced when the scrollbar has reached the edge. 629 } 630 } 631 ``` 632 633  634