• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Grid
2
3>  **NOTE**
4>
5>  - The APIs of this module are supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
6>
7>  - The column width and column gap in the grid layout are determined by the nearest parent component **GridContainer**. The component tree that uses grid attributes must contain one **GridContainer** or more.
8
9
10## Attributes
11
12
13| Name       | Type                                                    | Description                                                        |
14| ----------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
15| useSizeType | {<br>xs?: number \| { span: number, offset: number },<br>sm?: number \| { span: number, offset: number },<br>md?: number \| { span: number, offset: number },<br>lg?: number \| { span: number, offset: number }<br>} | Number of occupied columns and offset columns for a specific device width type. **span** indicates the number of occupied columns, and **offset** indicates the number of offset columns.<br>If the value is of the number type, only the number of columns can be set. If the value is in the format of {"span": 1, "offset": 0}, both the number of occupied columns and the number of offset columns need to be set.<br>- **xs** indicates that the device width type is **SizeType.XS**.<br>- **sm** indicates that the device width type is **SizeType.SM**.<br>- **md** indicates that the device width type is **SizeType.MD**.<br>- **lg** indicates that the device width type is **SizeType.LG**.|
16| gridSpan    | number                                                       | Default number of occupied columns, that is, the number of occupied columns when **span** in **useSizeType** is not set.<br>**NOTE**<br>If the **span** attribute is set, the component width is determined by the grid layout.<br>Default value: **1**|
17| gridOffset  | number                                                       | Default number of offset columns, that is, the number of offset columns in the start direction of the parent component (which is also the nth column that the component is in) when **offset** in **useSizeType** is not set.<br>**NOTE**<br>- After this attribute is set, the horizontal layout of the current component does not follow the original layout of the parent component. Instead, it offsets along the start direction of the parent component.<br>- Offset = (Column width + Gap) \* Number of columns.<br>- After this attribute is set, sibling components will be arranged relatively to this component, as in the relative layout.<br>Default value: **0**|
18
19
20## Example
21
22```ts
23// xxx.ets
24@Entry
25@Component
26struct GridContainerExample1 {
27  build() {
28    Column() {
29      Text('useSizeType').fontSize(15).fontColor(0xCCCCCC).width('90%')
30      GridContainer() {
31        Row({}) {
32          Row() {
33            Text('Left').fontSize(25)
34          }
35          .useSizeType({
36            xs: { span: 1, offset: 0 }, sm: { span: 1, offset: 0 },
37            md: { span: 1, offset: 0 }, lg: { span: 2, offset: 0 }
38          })
39          .height("100%")
40          .backgroundColor(0x66bbb2cb)
41
42          Row() {
43            Text('Center').fontSize(25)
44          }
45          .useSizeType({
46            xs: { span: 1, offset: 0 }, sm: { span: 2, offset: 1 },
47            md: { span: 5, offset: 1 }, lg: { span: 7, offset: 2 }
48          })
49          .height("100%")
50          .backgroundColor(0x66b6c5d1)
51
52          Row() {
53            Text('Right').fontSize(25)
54          }
55          .useSizeType({
56            xs: { span: 1, offset: 0 }, sm: { span: 1, offset: 3 },
57            md: { span: 2, offset: 6 }, lg: { span: 3, offset: 9 }
58          })
59          .height("100%")
60          .backgroundColor(0x66bbb2cb)
61        }
62        .height(200)
63
64      }
65      .backgroundColor(0xf1f3f5)
66      .margin({ top: 10 })
67
68      // Set the span and offset of the component separately. The resultant effect is equivalent to that achieved by using sm in useSizeType on the device of the sm type.
69      Text('gridSpan,gridOffset').fontSize(15).fontColor(0xCCCCCC).width('90%')
70      GridContainer() {
71        Row() {
72          Row() {
73            Text('Left').fontSize(25)
74          }
75          .gridSpan(1)
76          .height("100%")
77          .backgroundColor(0x66bbb2cb)
78
79          Row() {
80            Text('Center').fontSize(25)
81          }
82          .gridSpan(2)
83          .gridOffset(1)
84          .height("100%")
85          .backgroundColor(0x66b6c5d1)
86
87          Row() {
88            Text('Right').fontSize(25)
89          }
90          .gridSpan(1)
91          .gridOffset(3)
92          .height("100%")
93          .backgroundColor(0x66bbb2cb)
94        }.height(200)
95      }
96    }
97  }
98}
99```
100
101**Figure 1** Device width type SM
102
103![en-us_image_0000001256858405](figures/en-us_image_0000001256858405.png)
104
105**Figure 2** Device width type MD
106
107![en-us_image_0000001257058415](figures/en-us_image_0000001257058415.png)
108
109**Figure 3** Device width type LG
110
111![en-us_image_0000001212378416](figures/en-us_image_0000001212378416.png)
112
113**Figure 4** Setting gridSpan and gridOffset separately has the same effect as using SizeType for a specific device width type
114
115![gridSpan](figures/gridSpan.png)
116