Skip to content

Layout 栅格布局

Layout 组件提供了 24 列栅格系统,通过 Row(行)和 Col(列)组件的组合使用,可以轻松实现各种响应式布局。

导入

js
import Row from '@/components/layout/grid/Row.vue'
import Col from '@/components/layout/grid/Col.vue'

用法

基础用法

通过在 Col 上添加 span 属性设置列所占的宽度百分比。

vue
<template>
  <Row>
    <Col backgroundColor="primary" :span="8"><Text>span: 8</Text></Col>
    <Col backgroundColor="success" :span="8"><Text>span: 8</Text></Col>
    <Col backgroundColor="primary" :span="8"><Text>span: 8</Text></Col>
  </Row>
  <Row>
    <Col backgroundColor="primary" :span="4"><Text>span: 4</Text></Col>
    <Col backgroundColor="success" :offset="10" :span="10"><Text>offset: 10, span: 10</Text></Col>
  </Row>
  <Row>
    <Col backgroundColor="primary" :offset="12" :span="12"><Text>offset: 12, span: 12</Text></Col>
  </Row>
</template>

设置列元素间距

通过 gutter 属性可以设置列元素之间的间距,默认间距为 0。

vue
<template>
  <Row :gutter="20">
    <Col backgroundColor="primary" :span="8"><Text>span: 8</Text></Col>
    <Col backgroundColor="success" :span="8"><Text>span: 8</Text></Col>
    <Col backgroundColor="primary" :span="8"><Text>span: 8</Text></Col>
  </Row>
</template>

对齐方式

通过 justify 属性可以设置主轴上内容的对齐方式,等价于 flex 布局中的 justifyContent 属性。

vue
<template>
  <Row justify="center">
    <Col backgroundColor="primary" :span="6"><Text>span: 6</Text></Col>
    <Col backgroundColor="success" :span="6"><Text>span: 6</Text></Col>
    <Col backgroundColor="primary" :span="6"><Text>span: 6</Text></Col>
  </Row>
  <Row justify="flex-end">
    <Col backgroundColor="primary" :span="6"><Text>span: 6</Text></Col>
    <Col backgroundColor="success" :span="6"><Text>span: 6</Text></Col>
    <Col backgroundColor="primary" :span="6"><Text>span: 6</Text></Col>
  </Row>
  <Row justify="space-between">
    <Col backgroundColor="primary" :span="6"><Text>span: 6</Text></Col>
    <Col backgroundColor="success" :span="6"><Text>span: 6</Text></Col>
    <Col backgroundColor="primary" :span="6"><Text>span: 6</Text></Col>
  </Row>
  <Row justify="space-around">
    <Col backgroundColor="primary" :span="6"><Text>span: 6</Text></Col>
    <Col backgroundColor="success" :span="6"><Text>span: 6</Text></Col>
    <Col backgroundColor="primary" :span="6"><Text>span: 6</Text></Col>
  </Row>
</template>

API 参考

Row

栅格行组件,是 Col 组件的容器。

Props

名称说明类型必填默认值
gutter列元素之间的间距(单位为 px)number-0
wrap是否自动换行boolean-false
inline是否为行内元素boolean-false
flexflex 属性值number | string-1
justify主轴对齐方式string--
align交叉轴对齐方式string--
directionflex 方向string-'row'
padding边距设置number | number[]--
margin外边距设置number | number[]--
radius圆角大小number--
gap子元素间距number--
touchable是否可点击boolean-false
backgroundColor背景颜色string--
pressedColor按下时的背景颜色string--
activeOpacity按下时的透明度number--
width宽度string | number--
height高度string | number--
overflow溢出处理string--
innerStyle自定义样式Object--

Slots

名称说明
defaultRow 内容区域,用于放置 Col 组件

Col

栅格列组件,建议作为 Row 的子组件使用。

Props

名称说明类型必填默认值
span列元素宽度,取值范围 0-24number-0
offset列元素偏移距离,取值范围 0-24number-0
directionflex 方向string-'column'
justify主轴对齐方式string--
align交叉轴对齐方式string--
padding边距设置number | number[]--
margin外边距设置number | number[]--
radius圆角大小number--
gap子元素间距number--
touchable是否可点击boolean-false
backgroundColor背景颜色string--
pressedColor按下时的背景颜色string--
activeOpacity按下时的透明度number--
width宽度string | number--
height高度string | number--
overflow溢出处理string--
innerStyle自定义样式Object--

Slots

名称说明
defaultCol 内容区域

主题变量

Layout 组件继承了 FlexView 组件的主题变量,具体可参考 Flex 布局 的主题变量部分。