• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# GridRow
2
3栅格布局可以为布局提供规律性的结构,解决多尺寸多设备的动态布局问题,保证不同设备上各个模块的布局一致性。
4
5栅格容器组件,仅可以和栅格子组件([GridCol](ts-container-gridcol.md))在栅格布局场景中使用。
6
7>  **说明:**
8>
9> 该组件从API Version 9开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
10
11
12## 子组件
13
14可以包含GridCol子组件。
15
16
17## 接口
18GridRow(option?: GridRowOptions)
19
20从API version 9开始,该接口支持在ArkTS卡片中使用。
21
22**参数:**
23| 参数名 |类型|必填|说明|
24|-----|-----|----|----|
25| option | [GridRowOptions](#gridrowoptions对象说明) | 否  | 栅格布局子组件参数。 |
26
27## GridRowOptions对象说明
28| 参数名 |类型|必填|说明|
29|-----|-----|----|----|
30|columns| number \| [GridRowColumnOption](#gridrowcolumnoption) |  否  |设置布局列数。<br />默认值:12 |
31|gutter|[Length](ts-types.md#length) \| [GutterOption](#gutteroption)|   否  |栅格布局间距。<br />默认值:0 |
32|breakpoints|[BreakPoints](#breakpoints)|  否  |设置断点值的断点数列以及基于窗口或容器尺寸的相应参照。<br />默认值:<br />{<br />value: ["320vp", "600vp", "840vp"],<br />reference: BreakpointsReference.WindowSize<br />} |
33|direction|[GridRowDirection](#gridrowdirection枚举说明)|   否  |栅格布局排列方向。<br />默认值:GridRowDirection.Row |
34
35## GutterOption
36
37栅格布局间距类型,用于描述栅格子组件不同方向的间距。
38
39从API version 9开始,该接口支持在ArkTS卡片中使用。
40
41| 参数名   | 参数类型   | 必填   | 参数描述                                     |
42| ----- | ------ | ---- | ---------------------------------------- |
43| x  | [Length](ts-types.md#length) \| [GridRowSizeOption](#gridrowsizeoption) | 否   | 栅格子组件水平方向间距。    |
44| y  | [Length](ts-types.md#length) \| [GridRowSizeOption](#gridrowsizeoption) | 否   | 栅格子组件竖直方向间距。    |
45
46## GridRowColumnOption
47
48栅格在不同宽度设备类型下,栅格列数。
49
50从API version 9开始,该接口支持在ArkTS卡片中使用。
51
52| 参数名   | 参数类型   | 必填   | 参数描述                                     |
53| ----- | ------ | ---- | ---------------------------------------- |
54| xs  | number | 否    | 在栅格大小为xs的设备上,栅格容器组件的栅格列数。    |
55| sm  | number | 否    | 在栅格大小为sm的设备上,栅格容器组件的栅格列数。      |
56| md  | number | 否    | 在栅格大小为md的设备上,栅格容器组件的栅格列数。    |
57| lg  | number | 否    | 在栅格大小为lg的设备上,栅格容器组件的栅格列数。      |
58| xl  | number | 否    | 在栅格大小为xl的设备上,栅格容器组件的栅格列数。    |
59| xxl | number | 否    | 在栅格大小为xxl的设备上,栅格容器组件的栅格列数。    |
60
61**说明:**
62
63在GridRow栅格组件中,允许开发者使用breakpoints自定义修改[断点](../../../ui/arkts-layout-development-grid-layout.md#栅格系统断点)的取值范围,最多支持xs、sm、md、lg、xl、xxl六个断点,且名称不可修改。假设传入的数组是[n0, n1, n2, n3, n4],各个断点取值如下:
64|断点|取值范围|
65|---|-----------|
66|xs |[0, n0)    |
67|sm |[n0, n1)   |
68|md |[n1, n2)   |
69|lg |[n2, n3)   |
70|xl |[n3, n4)   |
71|xxl|[n4, INF)  |
72
73## GridRowSizeOption
74
75栅格在不同宽度设备类型下,gutter的大小。
76
77从API version 9开始,该接口支持在ArkTS卡片中使用。
78
79| 参数名   | 参数类型   | 必填   | 参数描述                                     |
80| ----- | ------ | ---- | ---------------------------------------- |
81| xs  | [Length](ts-types.md#length) | 否    | 在最小宽度类型设备上,栅格子组件的间距。    |
82| sm  | [Length](ts-types.md#length) | 否    | 在小宽度类型设备上,栅格子组件的间距。      |
83| md  | [Length](ts-types.md#length) | 否    | 在中等宽度类型设备上,栅格子组件的间距。    |
84| lg  | [Length](ts-types.md#length) | 否    | 在大宽度类型设备上,栅格子组件的间距。      |
85| xl  | [Length](ts-types.md#length) | 否    | 在特大宽度类型设备上,栅格子组件的间距。    |
86| xxl | [Length](ts-types.md#length) | 否    | 在超大宽度类型设备上,栅格子组件的间距。    |
87
88## BreakPoints
89
90设置栅格容器组件的断点。
91
92从API version 9开始,该接口支持在ArkTS卡片中使用。
93
94| 参数名   | 参数类型   | 必填   | 参数描述                                     |
95| ----- | ------ | ---- | ---------------------------------------- |
96| value  | Array&lt;string&gt; | 否  | 设置断点位置的单调递增数组。<br>默认值:["320vp", "600vp", "840vp"]    |
97| reference  | [BreakpointsReference](#breakpointsreference枚举说明) | 否    | 断点切换参照物。<br>默认值:BreakpointsReference.WindowSize |
98```ts
99  // 启用xs、sm、md共3个断点
100  breakpoints: {value: ["100vp", "200vp"]}
101  // 启用xs、sm、md、lg共4个断点,断点范围值必须单调递增
102  breakpoints: {value: ["320vp", "600vp", "840vp"]}
103  // 启用xs、sm、md、lg、xl共5个断点,断点范围数量不可超过断点可取值数量-1
104  breakpoints: {value: ["320vp", "600vp", "840vp", "1080vp"]}
105```
106
107## BreakpointsReference枚举说明
108
109从API version 9开始,该接口支持在ArkTS卡片中使用。
110
111| 名称 | 描述 |
112| -------- | -------- |
113| WindowSize | 以窗口为参照。 |
114| ComponentSize | 以容器为参照。 |
115
116## GridRowDirection枚举说明
117
118从API version 9开始,该接口支持在ArkTS卡片中使用。
119
120| 名称 | 描述 |
121| -------- | -------- |
122| Row | 栅格元素按照行方向排列。 |
123| RowReverse | 栅格元素按照逆序行方向排列。 |
124
125**说明:**
126* 栅格元素仅支持Row/RowReverse排列,不支持column/ColumnReverse方向排列。
127* 栅格子组件仅能通过span、offset计算子组件位置与大小。多个子组件span超过规定列数时自动换行。
128* 单个元素span大小超过最大列数时后台默认span为最大column数。
129* 新一行的Offset加上子组件的span超过总列数时,将下一个子组件在新的一行放置。
130* 例:Item1: GridCol({ span: 6}), Item2: GridCol({ span: 8, offset:11})
131
132|1      | 2     | 3     | 4     | 5     | 6     | 7     | 8     | 9     | 10    | 11    | 12    |
133| ----- | ------ | ---- | ---- | -----|-----|---------|--------|------|------- |------- |------- |
134| $\circ$ | $\circ$ | $\circ$ | $\circ$ | $\circ$|$\circ$| - |  - |  - |  -  | -  | -  |
135| -     | -     | -     | -     | -     |       |       |       |       |       |   |   |
136| $\circ$ | $\circ$ | $\circ$ | $\circ$ | $\circ$|$\circ$|$\circ$|$\circ$|  |   |   |   |
137
138## 属性
139
140除支持[通用属性](ts-universal-attributes-size.md)外,还支持以下属性:
141
142| 名称                       | 参数类型                            | 描述                                               |
143| ----------------------- | ----------------------------------- | ------------------------------------------- |
144| alignItems<sup>10+</sup>             | ItemAlign | 设置GridRow中的GridCol垂直主轴方向对齐方式,默认值:ItemAlign.Start<br/>**说明**:<br/>ItemAlign支持的枚举:ItemAlign.StartItemAlign.CenterItemAlign.EndItemAlign.Stretch。<br/>GridCol本身也可通过alignSelf([ItemAlign](ts-appendix-enums.md#itemalign))设置自身对齐方式。当上述两种对齐方式都设置时,以GridCol自身设置为准。<br/>从API version 10开始,该接口支持在ArkTS卡片中使用。 |
145
146
147## 事件
148
149### onBreakpointChange
150
151onBreakpointChange(callback: (breakpoints: string) => void)
152
153断点发生变化时触发回调。
154
155从API version 9开始,该接口支持在ArkTS卡片中使用。
156
157**参数:**
158
159| 参数名   | 参数类型   | 必填   | 说明   |
160| ----- | ------ | ---- | ---------------------------------------- |
161|breakpoints| string |否|取值为`"xs"`、`"sm"`、`"md"`、`"lg"`、`"xl"`、`"xxl"`。|
162
163## 示例
164
165```ts
166// xxx.ets
167@Entry
168@Component
169struct GridRowExample {
170  @State bgColors: Color[] = [Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Pink, Color.Grey, Color.Blue, Color.Brown]
171  @State currentBp: string = 'unknown'
172
173  build() {
174    Column() {
175      GridRow({
176        columns: 5,
177        gutter: { x: 5, y: 10 },
178        breakpoints: { value: ["400vp", "600vp", "800vp"],
179          reference: BreakpointsReference.WindowSize },
180        direction: GridRowDirection.Row
181      }) {
182        ForEach(this.bgColors, (color: Color) => {
183          GridCol({ span: { xs: 1, sm: 2, md: 3, lg: 4 }, offset: 0, order: 0 }) {
184            Row().width("100%").height("20vp")
185          }.borderColor(color).borderWidth(2)
186        })
187      }.width("100%").height("100%")
188      .onBreakpointChange((breakpoint) => {
189        this.currentBp = breakpoint
190      })
191    }.width('80%').margin({ left: 10, top: 5, bottom: 5 }).height(200)
192    .border({ color: '#880606', width: 2 })
193  }
194}
195```
196
197![figures/gridrow.png](figures/gridrow.png)
198