1e41f4b71Sopenharmony_ci# grid-col 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci> **说明:** 4e41f4b71Sopenharmony_ci> 从API version 5开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_cigrid-col是栅格布局容器grid-row的子容器组件。 7e41f4b71Sopenharmony_ci 8e41f4b71Sopenharmony_ci## 权限列表 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci无 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci## 子组件 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci支持。 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci## 属性 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci除支持[通用属性](js-components-common-attributes.md)外,还支持如下属性: 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci| 名称 | 类型 | 默认值 | 必填 | 描述 | 23e41f4b71Sopenharmony_ci| ------ | -------------- | ---- | ---- | ---------------------------------------- | 24e41f4b71Sopenharmony_ci| xs | number\|object | - | 否 | 在分辨率为xs模式下,设置该项占用列数与偏移列数,当值为number类型时,仅设置列数,也可通过object同时设置占用列数与偏移列数,如<br>`{"span": 1, "offset": 0}` | 25e41f4b71Sopenharmony_ci| sm | number\|object | - | 否 | 在分辨率为sm模式下,该项占用列数与偏移列数,当值为number类型时,仅设置列数,也可通过object同时设置占用列数与偏移列数。如<br>`{"span": 1, "offset": 0}` | 26e41f4b71Sopenharmony_ci| md | number\|object | - | 否 | 在分辨率为md模式下,该项占用列数与偏移列数,当值为number类型时,仅设置列数,也可通过object同时设置占用列数与偏移列数。如<br>`{"span": 1, "offset": 0}` | 27e41f4b71Sopenharmony_ci| lg | number\|object | - | 否 | 在分辨率为lg模式下,该项占用列数与偏移列数,当值为number类型时,仅设置列数,也可通过object同时设置占用列数与偏移列数。如<br>`{"span": 1, "offset": 0}` | 28e41f4b71Sopenharmony_ci| span | number | 1 | 否 | 在未设置明确断点时,默认占用列数 | 29e41f4b71Sopenharmony_ci| offset | number | 0 | 否 | 未设置具体分辨率模式下偏移时,当前元素延容器布局方向,默认偏移的列数 | 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci## 样式 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci除支持[通用样式](js-components-common-styles.md)外,还支持如下样式: 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci| 名称 | 类型 | 默认值 | 必填 | 描述 | 37e41f4b71Sopenharmony_ci| ----------------------------- | -------------- | ---------- | ---- | ---------------------------------------- | 38e41f4b71Sopenharmony_ci| flex-direction | string | row | 否 | flex容器主轴方向。可选项有:<br/>- column:垂直方向从上到下<br/>- row:水平方向从左到右 | 39e41f4b71Sopenharmony_ci| flex-wrap | string | nowrap | 否 | flex容器是单行还是多行显示,该值暂不支持动态修改。可选项有:<br/>- nowrap:不换行,单行显示。<br/>- wrap:换行,多行显示。 | 40e41f4b71Sopenharmony_ci| justify-content | string | flex-start | 否 | flex容器当前行的主轴对齐格式。可选项有:<br/>- flex-start:项目位于容器的开头。<br/>- flex-end:项目位于容器的结尾。<br/>- center:项目位于容器的中心。<br/>- space-between:项目位于各行之间留有空白的容器内。<br/>- space-around:项目位于各行之前、之间、之后都留有空白的容器内。 | 41e41f4b71Sopenharmony_ci| align-items | string | stretch | 否 | flex容器当前行的交叉轴对齐格式,可选值为:<br/>- stretch:弹性元素被在交叉轴方向被拉伸到与容器相同的高度或宽度。<br/>- flex-start:元素向交叉轴起点对齐。<br/>- flex-end:元素向交叉轴终点对齐。<br/>- center:元素在交叉轴居中。 | 42e41f4b71Sopenharmony_ci| align-content | string | flex-start | 否 | 交叉轴中有额外的空间时,多行内容对齐格式,可选值为:<br/>- flex-start:所有行从交叉轴起点开始填充。第一行的交叉轴起点边和容器的交叉轴起点边对齐。接下来的每一行紧跟前一行。<br/>- flex-end:所有行从交叉轴末尾开始填充。最后一行的交叉轴终点和容器的交叉轴终点对齐。同时所有后续行与前一个对齐。<br/>- center:所有行朝向容器的中心填充。每行互相紧挨,相对于容器居中对齐。容器的交叉轴起点边和第一行的距离相等于容器的交叉轴终点边和最后一行的距离。<br/>- space-between:所有行在容器中平均分布。相邻两行间距相等。容器的交叉轴起点边和终点边分别与第一行和最后一行的边对齐。<br/>- space-around:所有行在容器中平均分布,相邻两行间距相等。容器的交叉轴起点边和终点边分别与第一行和最后一行的距离是相邻两行间距的一半。 | 43e41f4b71Sopenharmony_ci| display | string | flex | 否 | 确定该元素视图框的类型,该值暂不支持动态修改。可选值为:<br/>- flex:弹性布局<br/>- grid:网格布局<br/>- none:不渲染此元素 | 44e41f4b71Sopenharmony_ci| grid-template-[columns\|rows] | string | 1行1列 | 否 | 用于设置当前网格布局行和列的数量,不设置时默认1行1列,仅当display为grid时生效。<br/>示例:如设置grid-template-columns为:<br/>- 50px 100px 60px:分三列,第一列50px,第二列100px,第三列60px;<br/>- 1fr 1fr 2fr:分三列,将父组件允许的宽分为4等份,第一列占1份,第二列占一份,第三列占2份;<br/>- 30% 20% 50%:分三列,将父组件允许的宽为基准,第一列占30%,第二列占20%,第三列占50%;<br/>- repeat(2,100px):分两列,第一列100px,第二列100px;<br/>- auto 1fr 1fr:分三列,第一列自适应内部子组件所需宽度,剩余空间分为两等份,第二列占一份,第三列占一份。 | 45e41f4b71Sopenharmony_ci| grid-[columns\|rows]-gap | <length> | 0 | 否 | 用于设置行与行的间距或者列与列的间距,也可以支持通过grid-gap设置相同的行列间距,仅当display为grid时生效。 | 46e41f4b71Sopenharmony_ci| grid-row-[start\|end] | number | - | 否 | 用于设置当前元素在网格布局中的起止行号,仅当父组件display样式为grid时生效(仅div支持display样式设置为grid)。 | 47e41f4b71Sopenharmony_ci| grid-column-[start\|end] | number | - | 否 | 用于设置当前元素在网格布局中的起止列号,仅当父组件display样式为grid时生效(仅div支持display样式设置为grid)。 | 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci> **说明:** 50e41f4b71Sopenharmony_ci> 不支持宽度相关样式。 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci## 事件 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci支持[通用事件](js-components-common-events.md)。 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci## 方法 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_ci支持[通用方法](js-components-common-methods.md)。 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci## 示例 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci```html 66e41f4b71Sopenharmony_ci<!-- index.hml --> 67e41f4b71Sopenharmony_ci<div class="container"> 68e41f4b71Sopenharmony_ci <grid-container id="mygrid" columns="5" gutter="20px" style="background-color: pink;"> 69e41f4b71Sopenharmony_ci <grid-row style="height:400px;justify-content:space-around;"> 70e41f4b71Sopenharmony_ci <grid-col span="2"> 71e41f4b71Sopenharmony_ci <div style="align-items: center;justify-content: center;height: 100%;width: 100%;"> 72e41f4b71Sopenharmony_ci <text style="color: dodgerblue;" onclick="getCol">Element text</text> 73e41f4b71Sopenharmony_ci </div> 74e41f4b71Sopenharmony_ci </grid-col> 75e41f4b71Sopenharmony_ci <grid-col span="3" style="background-color:orange;"> 76e41f4b71Sopenharmony_ci <div style="width: 100%;height: 100%;padding: 20px;align-items: center;"> 77e41f4b71Sopenharmony_ci <text onclick="getColWidth">Element text</text> 78e41f4b71Sopenharmony_ci </div> 79e41f4b71Sopenharmony_ci </grid-col> 80e41f4b71Sopenharmony_ci </grid-row> 81e41f4b71Sopenharmony_ci </grid-container> 82e41f4b71Sopenharmony_ci</div> 83e41f4b71Sopenharmony_ci``` 84e41f4b71Sopenharmony_ci 85e41f4b71Sopenharmony_ci```css 86e41f4b71Sopenharmony_ci/* index.css */ 87e41f4b71Sopenharmony_ci.container { 88e41f4b71Sopenharmony_ci flex-direction: column; 89e41f4b71Sopenharmony_ci padding-top: 80px; 90e41f4b71Sopenharmony_ci} 91e41f4b71Sopenharmony_ci``` 92e41f4b71Sopenharmony_ci 93e41f4b71Sopenharmony_ci```js 94e41f4b71Sopenharmony_ci// index.js 95e41f4b71Sopenharmony_ciimport promptAction from '@ohos.promptAction'; 96e41f4b71Sopenharmony_ciexport default { 97e41f4b71Sopenharmony_ci getCol(e) { 98e41f4b71Sopenharmony_ci this.$element('mygrid').getColumns(function (result) { 99e41f4b71Sopenharmony_ci promptAction.showToast({ 100e41f4b71Sopenharmony_ci message: e.target.id + ' result = ' + result, 101e41f4b71Sopenharmony_ci duration: 3000, 102e41f4b71Sopenharmony_ci }); 103e41f4b71Sopenharmony_ci }) 104e41f4b71Sopenharmony_ci }, 105e41f4b71Sopenharmony_ci getColWidth(e) { 106e41f4b71Sopenharmony_ci this.$element('mygrid').getColumnWidth(function (result) { 107e41f4b71Sopenharmony_ci promptAction.showToast({ 108e41f4b71Sopenharmony_ci message: e.target.id + ' result = ' + result, 109e41f4b71Sopenharmony_ci duration: 3000, 110e41f4b71Sopenharmony_ci }); 111e41f4b71Sopenharmony_ci }) 112e41f4b71Sopenharmony_ci } 113e41f4b71Sopenharmony_ci} 114e41f4b71Sopenharmony_ci``` 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ci 117