ts-universal-attributes-grid.md 3.7 KB
Newer Older
Z
zengyawen 已提交
1
# Grid
Z
zengyawen 已提交
2

Z
zengyawen 已提交
3

E
ester.zhou 已提交
4 5
> **NOTE**
>
Z
zengyawen 已提交
6
> - This attribute is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
E
ester.zhou 已提交
7
>
Z
zengyawen 已提交
8 9 10 11
> - 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.


## Required Permissions
Z
zengyawen 已提交
12 13 14

None

Z
zengyawen 已提交
15 16 17 18

## Attributes


E
ester.zhou 已提交
19
| Name | Type | Default Value | Description |
Z
zengyawen 已提交
20
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
21 22 23
| 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**. |
| gridSpan | number | 1 | 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. |
| gridOffset | number | 0 | 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/>> 1. 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/>> 2. Offset = (Column width + Gap) x Number of columns<br/>> 3. After this attribute is set, sibling components will be arranged relatively to this component, as in the relative layout. |
Z
zengyawen 已提交
24 25 26 27


## Example

Z
zengyawen 已提交
28

E
ester.zhou 已提交
29 30
```ts
// xxx.ets
Z
zengyawen 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
@Entry
@Component
struct GridContainerExample1 {
  build(){
    GridContainer() {
      Row({}) {
        Row() {
          Text('Left').fontSize(25)
        }
        .useSizeType({
          xs: { span: 1, offset: 0 }, sm: { span: 1, offset: 0 },
          md: { span: 1, offset: 0 }, lg: { span: 2, offset: 0 }
        })
        .height("100%")
        .backgroundColor(0x66bbb2cb)
        Row() {
          Text('Center').fontSize(25)
        }
        .useSizeType({
          xs: { span: 1, offset: 0 }, sm: { span: 2, offset: 1 },
          md: { span: 5, offset: 1 }, lg: { span: 7, offset: 2 }
        })
        .height("100%")
        .backgroundColor(0x66b6c5d1)
        Row() {
          Text('Right').fontSize(25)
        }
        .useSizeType({
          xs: { span: 1, offset: 0 }, sm: { span: 1, offset: 3 },
          md: { span: 2, offset: 6 }, lg: { span: 3, offset: 9 }
        })
        .height("100%")
        .backgroundColor(0x66bbb2cb)
      }
      .height(200)
    }
    .backgroundColor(0xf1f3f5)
    .margin({ top: 10 })
  }
}
```

E
ester.zhou 已提交
73 74
  **Figure 1** Device width type SM

Z
zengyawen 已提交
75
  ![en-us_image_0000001256858405](figures/en-us_image_0000001256858405.png)
Z
zengyawen 已提交
76

E
ester.zhou 已提交
77 78
  **Figure 2** Device width type MD

Z
zengyawen 已提交
79
  ![en-us_image_0000001257058415](figures/en-us_image_0000001257058415.png)
Z
zengyawen 已提交
80

E
ester.zhou 已提交
81 82
  **Figure 3** Device width type LG

Z
zengyawen 已提交
83
  ![en-us_image_0000001212378416](figures/en-us_image_0000001212378416.png)