1# 栅格设置 2 3> **说明:** 4> 5> - 从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 6> 7> - 栅格布局的列宽、列间距由距离最近的GridContainer父组件决定。使用栅格属性的组件树上至少需要有1个GridContainer容器组件。 8> 9> - gridSpan、gridOffset属性调用时其父组件或祖先组件必须是GridContainer。 10 11## 属性 12 13| 名称 | 参数类型 | 描述 | 14| ----------- | ------------------------------------------------------------ | ------------------------------------------------------------ | 15| useSizeType<sup>(deprecated) </sup> | {<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/>} | 设置在特定设备宽度类型下的占用列数和偏移列数,span: 占用列数; offset: 偏移列数。<br/>当值为number类型时,仅设置列数, 当格式如{"span": 1, "offset": 0}时,指同时设置占用列数与偏移列数。<br/>- xs: 指设备宽度类型为SizeType.XS时的占用列数和偏移列数。<br/>- sm: 指设备宽度类型为SizeType.SM时的占用列数和偏移列数。<br/>- md: 指设备宽度类型为SizeType.MD时的占用列数和偏移列数。<br/>- lg: 指设备宽度类型为SizeType.LG时的占用列数和偏移列数。<br/>该属性从API version 9开始废弃,推荐使用新组件[GridCol](ts-container-gridcol.md)、[GridRow](ts-container-gridrow.md)。 | 16| gridSpan | number | 默认占用列数,指useSizeType属性没有设置对应尺寸的列数(span)时,占用的栅格列数。<br/>**说明:**<br/>设置了栅格span属性,组件的宽度由栅格布局决定。<br>默认值:1<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 17| gridOffset | number | 默认偏移列数,指useSizeType属性没有设置对应尺寸的偏移(offset)时, 当前组件沿着父组件Start方向,偏移的列数,也就是当前组件位于第n列。<br/>**说明:**<br/>- 配置该属性后,当前组件在父组件水平方向的布局不再跟随父组件原有的布局方式,而是沿着父组件的Start方向偏移一定位移。<br/>- 偏移位移 = (列宽 + 间距)\* 列数。<br/>- 设置了偏移(gridOffset)的组件之后的兄弟组件会根据该组件进行相对布局,类似相对布局。<br>默认值:0<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 18 19## 示例 20 21<!--code_no_check--> 22 23```ts 24// xxx.ets 25@Entry 26@Component 27struct GridContainerExample1 { 28 build() { 29 Column() { 30 Text('useSizeType').fontSize(15).fontColor(0xCCCCCC).width('90%') 31 GridContainer() { 32 Row() { 33 Row() { 34 Text('Left').fontSize(25) 35 } 36 .useSizeType({ 37 xs: { span: 1, offset: 0 }, sm: { span: 1, offset: 0 }, 38 md: { span: 1, offset: 0 }, lg: { span: 2, offset: 0 } 39 }) 40 .height("100%") 41 .backgroundColor(0x66bbb2cb) 42 43 Row() { 44 Text('Center').fontSize(25) 45 } 46 .useSizeType({ 47 xs: { span: 1, offset: 0 }, sm: { span: 2, offset: 1 }, 48 md: { span: 5, offset: 1 }, lg: { span: 7, offset: 2 } 49 }) 50 .height("100%") 51 .backgroundColor(0x66b6c5d1) 52 53 Row() { 54 Text('Right').fontSize(25) 55 } 56 .useSizeType({ 57 xs: { span: 1, offset: 0 }, sm: { span: 1, offset: 3 }, 58 md: { span: 2, offset: 6 }, lg: { span: 3, offset: 9 } 59 }) 60 .height("100%") 61 .backgroundColor(0x66bbb2cb) 62 } 63 .height(200) 64 65 } 66 .backgroundColor(0xf1f3f5) 67 .margin({ top: 10 }) 68 69 // 单独设置组件的span和offset,在sm尺寸大小的设备上使用useSizeType中sm的数据实现一样的效果 70 Text('gridSpan,gridOffset').fontSize(15).fontColor(0xCCCCCC).width('90%') 71 GridContainer() { 72 Row() { 73 Row() { 74 Text('Left').fontSize(25) 75 } 76 .gridSpan(1) 77 .height("100%") 78 .backgroundColor(0x66bbb2cb) 79 80 Row() { 81 Text('Center').fontSize(25) 82 } 83 .gridSpan(2) 84 .gridOffset(1) 85 .height("100%") 86 .backgroundColor(0x66b6c5d1) 87 88 Row() { 89 Text('Right').fontSize(25) 90 } 91 .gridSpan(1) 92 .gridOffset(3) 93 .height("100%") 94 .backgroundColor(0x66bbb2cb) 95 }.height(200) 96 } 97 } 98 } 99} 100``` 101 102**图1** 设备宽度为SM 103 104 105 106**图2** 设备宽度为MD 107 108 109 110**图3** 设备宽度为LG 111 112 113 114**图4** 单独设置gridSpan和gridOffset在特定屏幕大小下的效果与useSizeType效果一致 115 116