• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Grid Layout
2
3The grid layout is an important layout in adaptive layout. It excels at dividing a page and defining proportion.
4You can implement a grid layout through the **[\<Grid>](../reference/arkui-ts/ts-container-grid.md)** container and the **[\<GridItem>](../reference/arkui-ts/ts-container-griditem.md)** child component.
5**\<Grid>** sets grid-wide parameters, and **\<GridItem>** defines parameters specific to grid items. The grid layout has the following features:
6
71. When the size of the container changes, all child components and their spacings are adjusted proportionally.
82. The numbers of rows and columns in the grid layout as well as the proportion of each row and column are customizable.
93. The row and column spacing of child components in the grid layout can be set.
104. A child component can span the specified number of rows or columns.
11
12
13
14## Grid Settings
15
16### Number and Proportion of Rows and Columns
17The number and proportion of rows and columns are set through the **columnsTemplate** and **rowTemplate** attributes.
18
19The following describes how to set **columnsTemplate**. The value of this attribute is a string consisting of multiple 'number+fr' entries separated by spaces. The number of entries is the number of columns in the grid layout. The number before **fr** is used to calculate the proportion of the column to the grid layout width, thereby determining the column width.
20
21```ts
22struct GridExample {
23  @State Number: Array<string> = ['1', '2', '3', '4']
24
25  build() {
26    Column({ space: 5 }) {
27      Grid() {
28        ForEach(this.Number, (num: string) => {
29          GridItem() {
30            Text(`Column ${num}`)
31              .fontSize(16)
32              .textAlign(TextAlign.Center)
33              .backgroundColor(0xd0d0d0)
34              .width('100%')
35              .height('100%')
36              .borderRadius(5)
37          }
38        })
39      }
40      .columnsTemplate('1fr 1fr 1fr 1fr')
41      .rowsTemplate('1fr')
42      .columnsGap(10)
43      .rowsGap(20)
44      .width('90%')
45      .backgroundColor(0xF0F0F0)
46      .height(100)
47    }.width('100%')
48  }
49}
50```
51
52Create a four-column grid, where each column has an equal width.
53
54```ts
55struct GridExample {
56  @State Number: Array<string> = ['1', '2', '3', '4']
57
58  build() {
59    Column({ space: 5 }) {
60      Grid() {
61        ForEach(this.Number, (num: string) => {
62          GridItem() {
63            Text(`Column ${num}`)
64              .fontSize(16)
65              .textAlign(TextAlign.Center)
66              .backgroundColor(0xd0d0d0)
67              .width('100%')
68              .height('100%')
69              .borderRadius(5)
70          }
71        })
72      }
73      .columnsTemplate('1fr 2fr 3fr 4fr')
74      .rowsTemplate('1fr')
75      .columnsGap(10)
76      .rowsGap(20)
77      .width('90%')
78      .backgroundColor(0xF0F0F0)
79      .height(100)
80    }.width('100%')
81  }
82}
83```
84
85Create a four-column grid, where the proportions of the columns are 1:2:3:4.
86
87```ts
88struct GridExample {
89  @State Number: Array<string> = ['1', '2', '3']
90
91  build() {
92    Column({ space: 5 }) {
93      Grid() {
94        ForEach(this.Number, (num: string) => {
95          GridItem() {
96            Text(`Column ${num}`)
97              .fontSize(16)
98              .textAlign(TextAlign.Center)
99              .backgroundColor(0xd0d0d0)
100              .width('100%')
101              .height('100%')
102              .borderRadius(5)
103          }
104        })
105      }
106      .columnsTemplate('4fr 2fr 3fr')
107      .rowsTemplate('1fr')
108      .columnsGap(10)
109      .rowsGap(20)
110      .width('90%')
111      .backgroundColor(0xF0F0F0)
112      .height(100)
113    }.width('100%')
114  }
115}
116```
117
118Create a three-column grid, where the proportions of the columns are 4:2:3.
119
120The effect is as follows:
121
122![](figures/columnTemplate.png)
123
124### Alignment
125
126Set **layoutDirection** to define how grid items are laid out along the main axis.
127The layout options are **Row**, **RowReverse**, **Column**, and **ColumnReverse**.
128The effect is as follows:
129
130![](figures/gridlayout.png)
131
132### Spacing Between Rows and Columns
133
134Set **columnsGap** to define the vertical spacing of the grid items and **rowsGap** to define the horizontal spacing.
135
136```ts
137Grid()
138.columnsTemplate('1fr 1fr 1fr 1fr')
139.columnsGap(10)
140.rowsGap(20)
141```
142
143![](figures/columnGap.png)
144
145In the preceding example, the horizontal and vertical spacings between grid items are 10 and 20, respectively.
146
147## GridItem Settings
148
149### Setting the Number of Rows and Columns per Grid Item
150
151The rows and columns of a grid layout are numbered in sequence, starting from 1.
152
153If a grid item spans multiple rows, use **rowStart** to set the start row number and **rowEnd** to set the end row number. If the value of **rowStart** is the same as that of **rowEnd**, the grid item occupies only one grid. The sample code is as follows:
154
155```ts
156Grid() {
157    GridItem() {
158      Text('5')
159        .fontSize(16)
160        .textAlign(TextAlign.Center)
161        .textStyle()
162    }.rowStart(2).rowEnd(3) // The grid item spans the second and third rows.
163
164    GridItem() {
165      Text('4')
166        .fontSize(16)
167        .textAlign(TextAlign.Center)
168        .textStyle()
169    }.columnStart(4).columnEnd(5) // The grid item spans the fourth and fifth columns.
170
171    GridItem() {
172      Text('6')
173        .fontSize(16)
174        .textAlign(TextAlign.Center)
175        .textStyle()
176    }.columnStart(2).columnEnd(4) // The grid item spans the second to fourth columns.
177
178    GridItem() {
179      Text('9')
180        .fontSize(16)
181        .textAlign(TextAlign.Center)
182        .textStyle()
183    }.columnStart(3).columnEnd(4)    // The grid item spans the third and fourth columns.
184}
185.columnsTemplate('1fr 1fr 1fr 1fr 1fr')
186.rowsTemplate('1fr 1fr 1fr')
187.columnsGap(10)
188.rowsGap(20)
189.width('90%')
190.backgroundColor(0xF0F0F0)
191.height('200vp')
192.layoutDirection(GridDirection.Column)
193```
194
195![](figures/griditem.png)
196
197## Example Scenario
198
199Use the grid layout to implement a calculator. The sample code is as follows:
200
201```ts
202@Entry
203@Component
204struct GridExample {
205  @State Number: Array<string> = ['1', '2', '3', '+', '4', '5', '6', '-', '7', '8', '9', '*', '0', '.', '/']
206
207  @Styles textStyle(){
208    .backgroundColor(0xd0d0d0)
209    .width('100%')
210    .height('100%')
211    .borderRadius(5)
212  }
213
214  build() {
215    Column({ space: 5 }) {
216      Grid() {
217        GridItem() {
218          Text('0')
219            .fontSize(30)
220            .textStyle()
221        }.columnStart(1).columnEnd(4)
222
223        GridItem() {
224          Text ('Clear')
225            .fontSize(16)
226            .textAlign(TextAlign.Center)
227            .textStyle()
228        }.columnStart(1).columnEnd(2)
229
230        GridItem() {
231          Text ('Back')
232            .fontSize(16)
233            .textAlign(TextAlign.Center)
234            .textStyle()
235        }.columnStart(3).columnEnd(4)
236
237        ForEach(this.Number, (day: string) => {
238          if (day === '0') {
239            GridItem() {
240              Text(day)
241                .fontSize(16)
242                .textAlign(TextAlign.Center)
243                .textStyle()
244            }.columnStart(1).columnEnd(2)
245          } else {
246            GridItem() {
247              Text(day)
248                .fontSize(16)
249                .textAlign(TextAlign.Center)
250                .textStyle()
251            }
252          }
253        })
254      }
255      .columnsTemplate('1fr 1fr 1fr 1fr')
256      .rowsTemplate('2fr 1fr 1fr 1fr 1fr 1fr')
257      .columnsGap(10)
258      .rowsGap(15)
259      .width('90%')
260      .backgroundColor(0xF0F0F0)
261      .height('70%')
262    }.width('100%').margin({ top: 5 })
263  }
264}
265```
266
267Below you can see how the calculator displays on a large-screen device.
268
269![](figures/gridExp1.png)
270
271Below you can see how the calculator displays on a small-screen device.
272
273![](figures/gridExp2.png)
274