1# Linear Layout 2 3The linear layout is the most commonly used layout in development. The child components in a linear layout are arranged one by one in a linear direction, either vertically or horizontally. 4 5You can implement a linear layout through the **[\<Row>](../reference/arkui-ts/ts-container-row.md)** and **[\<Column>](../reference/arkui-ts/ts-container-column.md)** containers. In the **\<Column>** container, child components are arranged vertically. In the **\<Row>** container, child components are arranged horizontally. 6 7## Linear Layout Orientation 8 9The orientation of a linear layout is subject to the container: **\<Row>** or **\<Column>**. You can use the container attributes to adjust the spacing between child components and the horizontal and vertical alignment modes. 101. The **space** attribute sets the spacing between child components so that they are evenly spaced along the main axis. 112. The **alignItems** attribute sets the alignment mode of the child components along the cross axis, which is consistent across screens of various sizes. When the cross axis is vertical, the value type is [VerticalAlign](../reference/arkui-ts/ts-appendix-enums.md#verticalalign). When the cross axis is horizontal, the value type is [HorizontalAlign](../reference/arkui-ts/ts-appendix-enums.md#horizontalalign). 123. The **justifyContent** attribute sets the alignment mode of the child components along the main axis, implementing adaptive layout. The value type is [FlexAlign](../reference/arkui-ts/ts-appendix-enums.md#flexalign). 13 14The table below provides the usage and demo effects. 15 16|Attribute|Description|Row Effect|Column Effect| 17|------|---------------------------|----------------------------|---------------------------| 18|space |- Horizontal spacing between child components in the horizontal layout<br> - Vertical spacing between child components in the vertical layout|  |  | 19|alignItems |Alignment mode of child components along the cross axis of the container.| || 20|justifyContent |Alignment mode of child components along the main axis of the container.| || 21 22## Adaptive Stretching 23 24In linear layout, the **[\<Blank>](../reference/arkui-ts/ts-basic-components-blank.md)** component is commonly used to automatically fill blank space along the main axis of the container, so as to achieve adaptive stretching. 25 26```ts 27@Entry 28@Component 29struct BlankExample { 30 build() { 31 Column() { 32 Row() { 33 Text('Bluetooth').fontSize(18) 34 Blank() 35 Toggle({ type: ToggleType.Switch, isOn: true }) 36 }.backgroundColor(0xFFFFFF).borderRadius(15).padding({ left: 12 }).width('100%') 37 }.backgroundColor(0xEFEFEF).padding(20).width('100%') 38 } 39} 40``` 41 42 43 44## Adaptive Scaling 45 46Adaptive scaling means that child components scale according to the preset ratio to adapt to the container size on devices of various sizes. Below are the methods to implement adaptive scaling in a linear layout: 47 481. When the size of the parent container is determined, child components are laid out along the main axis based on the **layoutWeight** attribute settings, with the size settings of the child components ignored. In this way, the child components are adaptively scaled to fill up remaining space, regardless of the screen size. 49 50 ```ts 51 @Entry 52 @Component 53 struct layoutWeightExample { 54 build() { 55 Column() { 56 Text('1:2:3').width('100%') 57 Row() { 58 Column() { 59 Text('layoutWeight(1)') 60 .textAlign(TextAlign.Center) 61 }.layoutWeight(2).backgroundColor(0xffd306).height('100%') 62 63 Column() { 64 Text('layoutWeight(2)') 65 .textAlign(TextAlign.Center) 66 }.layoutWeight(4).backgroundColor(0xffed97).height('100%') 67 68 Column() { 69 Text('layoutWeight(6)') 70 .textAlign(TextAlign.Center) 71 }.layoutWeight(6).backgroundColor(0xffd306).height('100%') 72 73 }.backgroundColor(0xffd306).height('30%') 74 75 Text('2:5:3').width('100%') 76 Row() { 77 Column() { 78 Text('layoutWeight(2)') 79 .textAlign(TextAlign.Center) 80 }.layoutWeight(2).backgroundColor(0xffd306).height('100%') 81 82 Column() { 83 Text('layoutWeight(5)') 84 .textAlign(TextAlign.Center) 85 }.layoutWeight(5).backgroundColor(0xffed97).height('100%') 86 87 Column() { 88 Text('layoutWeight(3)') 89 .textAlign(TextAlign.Center) 90 }.layoutWeight(3).backgroundColor(0xffd306).height('100%') 91 }.backgroundColor(0xffd306).height('30%') 92 } 93 } 94 } 95 ``` 96 97  98 99 1003. When the size of the parent container is determined, set the widths of the child component and sibling components in percentage. 101 102 ```ts 103 @Entry 104 @Component 105 struct WidthExample { 106 build() { 107 Column() { 108 Row() { 109 Column() { 110 Text('left width 20%') 111 .textAlign(TextAlign.Center) 112 }.width('20%').backgroundColor(0xffd306).height('100%') 113 114 Column() { 115 Text('center width 50%') 116 .textAlign(TextAlign.Center) 117 }.width('50%').backgroundColor(0xffed97).height('100%') 118 119 Column() { 120 Text('right width 30%') 121 .textAlign(TextAlign.Center) 122 }.width('30%').backgroundColor(0xffd306).height('100%') 123 }.backgroundColor(0xffd306).height('30%') 124 } 125 } 126 } 127 ``` 128 129  130 131 In the preceding example, the proportion of child components remains unchanged across devices of different sizes. 132 133## Positioning 134- Relative layout 135 136 You can use the **[offset](../reference/arkui-ts/ts-universal-attributes-location.md)** attribute to set the offset of a component relative to itself, thereby implmenting relative layout. Setting this attribute does not affect the layout of the parent container. It only adjusts the component position during drawing. The linear layout and offset can work together to meet most layout development requirements. 137 138 ```ts 139 @Entry 140 @Component 141 struct OffsetExample { 142 @Styles eleStyle() { 143 .size({ width: 120, height: '50' }) 144 .backgroundColor(0xbbb2cb) 145 .border({ width: 1 }) 146 } 147 148 build() { 149 Column({ space: 20 }) { 150 Row() { 151 Text('1').size({ width: '15%', height: '50' }).backgroundColor(0xdeb887).border({ width: 1 }).fontSize(16) 152 Text('2 offset(15, 30)') 153 .eleStyle() 154 .fontSize(16) 155 .align(Alignment.Start) 156 .offset({ x: 15, y: 30 }) 157 Text('3').size({ width: '15%', height: '50' }).backgroundColor(0xdeb887).border({ width: 1 }).fontSize(16) 158 Text('4 offset(-10%, 20%)') 159 .eleStyle() 160 .fontSize(16) 161 .offset({ x: '-5%', y: '20%' }) 162 }.width('90%').height(150).border({ width: 1, style: BorderStyle.Dashed }) 163 } 164 .width('100%') 165 .margin({ top: 25 }) 166 } 167 } 168 ``` 169 170  171 172 173- Absolute layout 174 175 In linear layout, you can use the **[position](../reference/arkui-ts/ts-universal-attributes-location.md)** attribute to set the offset of the upper left corner of a component relative to the upper left corner of the parent container, thereby implementing absolute layout. Absolute layout offers poorer adaptability to screen sizes than relative layout. 176 177 ```ts 178 @Entry 179 @Component 180 struct PositionExample { 181 @Styles eleStyle(){ 182 .backgroundColor(0xbbb2cb) 183 .border({ width: 1 }) 184 .size({ width: 120, height: 50 }) 185 } 186 187 build() { 188 Column({ space: 20 }) { 189 // Set the offset of the upper left corner of the child component relative to the upper left corner of the parent container. 190 Row() { 191 Text('position(30, 10)') 192 .eleStyle() 193 .fontSize(16) 194 .position({ x: 10, y: 10 }) 195 196 Text('position(50%, 70%)') 197 .eleStyle() 198 .fontSize(16) 199 .position({ x: '50%', y: '70%' }) 200 201 Text('position(10%, 90%)') 202 .eleStyle() 203 .fontSize(16) 204 .position({ x: '10%', y: '80%' }) 205 }.width('90%').height('100%').border({ width: 1, style: BorderStyle.Dashed }) 206 } 207 .width('90%').margin(25) 208 } 209 } 210 ``` 211 212  213 214 215## Adaptive Extension 216 217Adaptive extension allows users to drag the scrollbar to display content when the content amount displayed on a page is subject to the screen size and the content extends beyond the viewport. Below are the methods to implement adaptive extension in a linear layout: 218 219 220- **\<List>** component 221 222 If the list items cannot be fully displayed on one screen, you can use the scrollbar to let users view the list items in full. Use the **scrollBar** attribute to set the scrollbar status, and the **edgeEffect** to set the rebound effect when the scrollbar has reached the edge. 223 224 225 Vertical list: 226 ```ts 227 @Entry 228 @Component 229 struct ListExample1 { 230 @State arr: string[] = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15"] 231 @State alignListItem: ListItemAlign = ListItemAlign.Start 232 233 build() { 234 Column() { 235 List({ space: 20, initialIndex: 0 }) { 236 ForEach(this.arr, (item) => { 237 ListItem() { 238 Text('' + item) 239 .width('100%') 240 .height(100) 241 .fontSize(16) 242 .textAlign(TextAlign.Center) 243 .borderRadius(10) 244 .backgroundColor(0xFFFFFF) 245 } 246 .border({ width: 2, color: Color.Green }) 247 }, item => item) 248 } 249 .border({ width: 2, color: Color.Red, style: BorderStyle.Dashed }) 250 .scrollBar (BarState.On) // ScrollBar status 251 .edgeEffect(EdgeEffect.Spring) // Rebound effect when the scrollbar has reached the edge 252 253 }.width('100%').height('100%').backgroundColor(0xDCDCDC).padding(20) 254 } 255 } 256 ``` 257 258  259 260 261 Horizontal List: 262 263 ```ts 264 @Entry 265 @Component 266 struct ListExample2 { 267 @State arr: string[] = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15"] 268 @State alignListItem: ListItemAlign = ListItemAlign.Start 269 270 build() { 271 Column() { 272 List({ space: 20, initialIndex: 0 }) { 273 ForEach(this.arr, (item) => { 274 ListItem() { 275 Text('' + item) 276 .height('100%') 277 .width(100) 278 .fontSize(16) 279 .textAlign(TextAlign.Center) 280 .borderRadius(10) 281 .backgroundColor(0xFFFFFF) 282 } 283 .border({ width: 2, color: Color.Green }) 284 }, item => item) 285 } 286 .border({ width: 2, color: Color.Red, style: BorderStyle.Dashed }) 287 .scrollBar (BarState.On) // Scrollbar status. 288 .edgeEffect(EdgeEffect.Spring) // Rebound effect when the scrollbar has reached the edge. 289 .listDirection(Axis.Horizontal) // Horizontal layout. 290 }.width('100%').height('100%').backgroundColor(0xDCDCDC).padding(20) 291 } 292 } 293 ``` 294 295  296 297- **\<Scroll>** component 298 299 In linear layout, the **\<Scroll>** component scrolls the content when the layout size of a component exceeds the size of its parent component. You can wrap a **\<Scroll>** component at the outer layer of the **\<Column>** or **\<Row>** component. 300 301 Vertical Scroll: 302 303 ```ts 304 @Entry 305 @Component 306 struct ScrollExample { 307 scroller: Scroller = new Scroller(); 308 private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; 309 310 build() { 311 Scroll(this.scroller) { 312 Column() { 313 ForEach(this.arr, (item) => { 314 Text(item.toString()) 315 .width('90%') 316 .height(150) 317 .backgroundColor(0xFFFFFF) 318 .borderRadius(15) 319 .fontSize(16) 320 .textAlign(TextAlign.Center) 321 .margin({ top: 10 }) 322 }, item => item) 323 }.width('100%') 324 } 325 .backgroundColor(0xDCDCDC) 326 .scrollable(ScrollDirection.Vertical) // Vertical scrolling. 327 .scrollBar(BarState.On) // Scrollbar status. 328 .scrollBarColor(Color.Gray) // Scrollbar color. 329 .scrollBarWidth(30) // Scrollbar width. 330 .edgeEffect(EdgeEffect.Spring) // Rebound effect when the scrollbar has reached the edge. 331 } 332 } 333 ``` 334 335  336 337 Horizontal scrolling: 338 339 ```ts 340 @Entry 341 @Component 342 struct ScrollExample { 343 scroller: Scroller = new Scroller(); 344 private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; 345 346 build() { 347 Scroll(this.scroller) { 348 Row() { 349 ForEach(this.arr, (item) => { 350 Text(item.toString()) 351 .height('90%') 352 .width(150) 353 .backgroundColor(0xFFFFFF) 354 .borderRadius(15) 355 .fontSize(16) 356 .textAlign(TextAlign.Center) 357 .margin({ left: 10 }) 358 }, item => item) 359 }.height('100%') 360 } 361 .backgroundColor(0xDCDCDC) 362 .scrollable(ScrollDirection.Horizontal) // Horizontal scrolling. 363 .scrollBar(BarState.On) // Scrollbar status. 364 .scrollBarColor(Color.Gray) // Scrollbar color 365 .scrollBarWidth(30) // Scrollbar width. 366 .edgeEffect(EdgeEffect.Spring) // Rebound effect when the scrollbar has reached the edge. 367 } 368 } 369 ``` 370  371