Flex Flex布局
定义/Definition
flexbox 布局
规则 / Rule
Flex布局的相关知识请先阅读阮一峰的博客Flex 布局教程## 代码演示
基本
基本,子元素FLex.Item,设置flex=1
import { Flex, Button, WingBlank, WhiteSpace } from 'antd-mobile';
let FlexExample = React.createClass({
render() {
return (
<div className="button-container">
<WhiteSpace />
<WingBlank>
<Flex>
<Flex.Item>
<Button>2列</Button>
</Flex.Item>
<Flex.Item>
<Button type="primary">2列</Button>
</Flex.Item>
</Flex>
</WingBlank>
<WhiteSpace />
<WingBlank>
<Flex>
<Flex.Item>
<Button size="small">3列</Button>
</Flex.Item>
<Flex.Item>
<Button type="primary" size="small">3列</Button>
</Flex.Item>
<Flex.Item>
<Button size="small">3列</Button>
</Flex.Item>
</Flex>
</WingBlank>
<WhiteSpace />
<WingBlank>
<Flex>
<Flex.Item>
<Button>4列</Button>
</Flex.Item>
<Flex.Item>
<Button type="primary">4列</Button>
</Flex.Item>
<Flex.Item>
<Button>4列</Button>
</Flex.Item>
<Flex.Item>
<Button type="primary">4列</Button>
</Flex.Item>
</Flex>
</WingBlank>
<WhiteSpace />
<WingBlank>
<Flex>
<Flex.Item>
<Button size="small">5列</Button>
</Flex.Item>
<Flex.Item>
<Button type="primary">5列</Button>
</Flex.Item>
<Flex.Item>
<Button size="small">5列</Button>
</Flex.Item>
<Flex.Item>
<Button type="primary">5列</Button>
</Flex.Item>
<Flex.Item>
<Button type="primary">5列</Button>
</Flex.Item>
</Flex>
</WingBlank>
<WhiteSpace />
</div>
);
},
});
ReactDOM.render(<FlexExample />, mountNode);
排列方向
direction="row/row-reverse/column/column-reverse"
import { Flex, List, Button, WhiteSpace } from 'antd-mobile';
let FlexExample = React.createClass({
render() {
return (
<div className="button-container">
<WhiteSpace />
<List>
<List.Header>row:主轴为水平方向,起点在左端</List.Header>
</List>
<Flex>
<Button size="small" type="primary" style={{ margin: '2px' }}>按钮1</Button>
<Button size="small" style={{ margin: '2px' }}>按钮2</Button>
<Button size="small" type="primary" style={{ margin: '2px' }}>按钮3</Button>
<Button size="small" style={{ margin: '2px' }}>按钮4</Button>
</Flex>
<List>
<List.Header>row-reverse:主轴为水平方向,起点在右端</List.Header>
</List>
<Flex
direction="row-reverse"
>
<Button size="small" type="primary" style={{ margin: '2px' }}>按钮1</Button>
<Button size="small" style={{ margin: '2px' }}>按钮2</Button>
<Button size="small" type="primary" style={{ margin: '2px' }}>按钮3</Button>
<Button size="small" style={{ margin: '2px' }}>按钮4</Button>
</Flex>
<List>
<List.Header>column:主轴为垂直方向,起点在上沿</List.Header>
</List>
<Flex
direction="column"
>
<Button size="small" type="primary" style={{ margin: '2px' }}>按钮1</Button>
<Button size="small" style={{ margin: '2px' }}>按钮2</Button>
<Button size="small" type="primary" style={{ margin: '2px' }}>按钮3</Button>
<Button size="small" style={{ margin: '2px' }}>按钮4</Button>
</Flex>
<List>
<List.Header>column-reverse:主轴为垂直方向,起点在下沿</List.Header>
</List>
<Flex
direction="column-reverse"
>
<Button size="small" type="primary" style={{ margin: '2px' }}>按钮1</Button>
<Button size="small" style={{ margin: '2px' }}>按钮2</Button>
<Button size="small" type="primary" style={{ margin: '2px' }}>按钮3</Button>
<Button size="small" style={{ margin: '2px' }}>按钮4</Button>
</Flex>
<WhiteSpace />
</div>
);
},
});
ReactDOM.render(<FlexExample />, mountNode);
换行
wrap="nowrap/wrap/wrap-reverse"
import { Flex, List, Button, WhiteSpace } from 'antd-mobile';
let FlexExample = React.createClass({
render() {
return (
<div className="button-container">
<WhiteSpace />
<List>
<List.Header>nowrap(默认):不换行</List.Header>
</List>
<Flex>
<Button size="small" type="primary" style={{ margin: '2px', width: '40px' }}>1</Button>
<Button size="small" style={{ margin: '2px', width: '40px' }}>2</Button>
<Button size="small" type="primary" style={{ margin: '2px', width: '40px' }}>3</Button>
<Button size="small" style={{ margin: '2px', width: '40px' }}>4</Button>
<Button size="small" type="primary" style={{ margin: '2px', width: '40px' }}>5</Button>
<Button size="small" style={{ margin: '2px', width: '40px' }}>6</Button>
<Button size="small" type="primary" style={{ margin: '2px', width: '40px' }}>7</Button>
<Button size="small" style={{ margin: '2px', width: '40px' }}>8</Button>
<Button size="small" type="primary" style={{ margin: '2px', width: '40px' }}>9</Button>
<Button size="small" style={{ margin: '2px', width: '40px' }}>10</Button>
<Button size="small" type="primary" style={{ margin: '2px', width: '40px' }}>11</Button>
<Button size="small" style={{ margin: '2px', width: '40px' }}>12</Button>
</Flex>
<List>
<List.Header>wrap:换行,第一行在上方</List.Header>
</List>
<Flex
wrap="wrap"
>
<Button size="small" type="primary" style={{ margin: '2px', width: '40px' }}>1</Button>
<Button size="small" style={{ margin: '2px', width: '40px' }}>2</Button>
<Button size="small" type="primary" style={{ margin: '2px', width: '40px' }}>3</Button>
<Button size="small" style={{ margin: '2px', width: '40px' }}>4</Button>
<Button size="small" type="primary" style={{ margin: '2px', width: '40px' }}>5</Button>
<Button size="small" style={{ margin: '2px', width: '40px' }}>6</Button>
<Button size="small" type="primary" style={{ margin: '2px', width: '40px' }}>7</Button>
<Button size="small" style={{ margin: '2px', width: '40px' }}>8</Button>
<Button size="small" type="primary" style={{ margin: '2px', width: '40px' }}>9</Button>
<Button size="small" style={{ margin: '2px', width: '40px' }}>10</Button>
<Button size="small" type="primary" style={{ margin: '2px', width: '40px' }}>11</Button>
<Button size="small" style={{ margin: '2px', width: '40px' }}>12</Button>
</Flex>
<List>
<List.Header>wrap-reverse:换行,第一行在下方</List.Header>
</List>
<Flex
wrap="wrap-reverse"
>
<Button size="small" type="primary" style={{ margin: '2px', width: '40px' }}>1</Button>
<Button size="small" style={{ margin: '2px', width: '40px' }}>2</Button>
<Button size="small" type="primary" style={{ margin: '2px', width: '40px' }}>3</Button>
<Button size="small" style={{ margin: '2px', width: '40px' }}>4</Button>
<Button size="small" type="primary" style={{ margin: '2px', width: '40px' }}>5</Button>
<Button size="small" style={{ margin: '2px', width: '40px' }}>6</Button>
<Button size="small" type="primary" style={{ margin: '2px', width: '40px' }}>7</Button>
<Button size="small" style={{ margin: '2px', width: '40px' }}>8</Button>
<Button size="small" type="primary" style={{ margin: '2px', width: '40px' }}>9</Button>
<Button size="small" style={{ margin: '2px', width: '40px' }}>10</Button>
<Button size="small" type="primary" style={{ margin: '2px', width: '40px' }}>11</Button>
<Button size="small" style={{ margin: '2px', width: '40px' }}>12</Button>
</Flex>
</div>
);
},
});
ReactDOM.render(<FlexExample />, mountNode);
主轴对齐方式
justify="start/end/center/between/around"
import { Flex, List, Button, WhiteSpace } from 'antd-mobile';
let FlexExample = React.createClass({
render() {
return (
<div className="button-container">
<WhiteSpace />
<List>
<List.Header>start(默认): 左对齐</List.Header>
</List>
<Flex>
<Button size="small" type="primary" style={{ width: '70px', margin: '2px' }}>按钮1</Button>
<Button size="small" style={{ width: '70px', margin: '2px' }}>按钮2</Button>
<Button size="small" type="primary" style={{ width: '70px', margin: '2px' }}>按钮3</Button>
</Flex>
<List>
<List.Header>end:右对齐</List.Header>
</List>
<Flex
justify="end"
>
<Button size="small" type="primary" style={{ width: '70px', margin: '2px' }}>按钮1</Button>
<Button size="small" style={{ width: '70px', margin: '2px' }}>按钮2</Button>
<Button size="small" type="primary" style={{ width: '70px', margin: '2px' }}>按钮3</Button>
</Flex>
<List>
<List.Header>center:居中</List.Header>
</List>
<Flex
justify="center"
>
<Button size="small" type="primary" style={{ width: '70px', margin: '2px' }}>按钮1</Button>
<Button size="small" style={{ width: '70px', margin: '2px' }}>按钮2</Button>
<Button size="small" type="primary" style={{ width: '70px', margin: '2px' }}>按钮3</Button>
</Flex>
<List>
<List.Header>between:两端对齐,项目之间的间隔都相等</List.Header>
</List>
<Flex
justify="between"
>
<Button size="small" type="primary" style={{ width: '70px', margin: '2px' }}>按钮1</Button>
<Button size="small" style={{ width: '70px', margin: '2px' }}>按钮2</Button>
<Button size="small" type="primary" style={{ width: '70px', margin: '2px' }}>按钮3</Button>
</Flex>
<List>
<List.Header>around:每个项目两侧的间隔相等。所以,项目之间的间隔比项目与边框的间隔大一倍。</List.Header>
</List>
<Flex
justify="around"
>
<Button size="small" type="primary" style={{ width: '70px', margin: '2px' }}>按钮1</Button>
<Button size="small" style={{ width: '70px', margin: '2px' }}>按钮2</Button>
<Button size="small" type="primary" style={{ width: '70px', margin: '2px' }}>按钮3</Button>
</Flex>
</div>
);
},
});
ReactDOM.render(<FlexExample />, mountNode);
交叉轴对齐方式
align="start/end/center/baseline/stretch"
import { Flex, Button, List, WingBlank, WhiteSpace } from 'antd-mobile';
let FlexExample = React.createClass({
render() {
return (
<div className="button-container">
<WhiteSpace />
<WingBlank>
<List>
<List.Header>start:交叉轴的起点对齐</List.Header>
</List>
<Flex align="start">
<Flex.Item>
<Button type="primary">3列</Button>
</Flex.Item>
<Flex.Item>
<Button size="small">3列</Button>
</Flex.Item>
</Flex>
</WingBlank>
<WhiteSpace />
<WingBlank>
<List>
<List.Header>end:交叉轴的终点对齐</List.Header>
</List>
<Flex align="end">
<Flex.Item>
<Button type="primary">3列</Button>
</Flex.Item>
<Flex.Item>
<Button size="small">3列</Button>
</Flex.Item>
</Flex>
</WingBlank>
<WhiteSpace />
<WingBlank>
<List>
<List.Header>center(默认):交叉轴的中点对齐</List.Header>
</List>
<Flex align="center">
<Flex.Item>
<Button type="primary">3列</Button>
</Flex.Item>
<Flex.Item>
<Button size="small">3列</Button>
</Flex.Item>
<Flex.Item>
<Button type="primary">3列</Button>
</Flex.Item>
<Flex.Item>
<Button size="small">3列</Button>
</Flex.Item>
</Flex>
</WingBlank>
<WhiteSpace />
<WingBlank>
<List>
<List.Header>baseline:项目的第一行文字的基线对齐</List.Header>
</List>
<Flex align="baseline">
<Flex.Item>
<Button type="primary">3列</Button>
</Flex.Item>
<Flex.Item>
<Button size="small">3列</Button>
</Flex.Item>
<Flex.Item>
<Button type="primary">3列</Button>
</Flex.Item>
<Flex.Item>
<Button size="small">3列</Button>
</Flex.Item>
</Flex>
</WingBlank>
<WhiteSpace />
<WingBlank>
<List>
<List.Header>stretch:如果项目未设置高度或设为auto,将占满整个容器的高度</List.Header>
</List>
<Flex align="stretch">
<Flex.Item style={{ border: '1px solid red' }}>
<Button type="primary">3列</Button>
</Flex.Item>
<Flex.Item style={{ border: '1px solid red' }}>
<Button size="small">3列</Button>
</Flex.Item>
<Flex.Item style={{ border: '1px solid red' }}>
<Button type="primary">3列</Button>
</Flex.Item>
<Flex.Item style={{ border: '1px solid red' }}>
<Button size="small">3列</Button>
</Flex.Item>
</Flex>
</WingBlank>
<WhiteSpace />
</div>
);
},
});
ReactDOM.render(<FlexExample />, mountNode);
API
Flex
成员 | 说明 | 类型 | 可选值 | 默认值 |
---|
direction | Flex子元素的排列方向 | String | row (水平方向,起点在左端), row-reverse (水平方向,起点在右端), column (垂直方向,起点在上沿), column-reverse (垂直方向,起点在下沿),RN仅支持row ,column | row |
wrap | Flex 子元素的换行方式 | String | nowrap (不换行), wrap (换行,第一行在上方), wrap-reverse (换行,第一行在下方),RN仅支持nowrap ,wrap | nowrap |
justify | Flex 子元素在主轴上的对齐方式 | String | start , end , center , between , around ),RN全部支持 | start |
align | Flex 子元素在交叉轴上的对齐方式, top和start等价,middle和center等价,bottom和end等价, 优先使用后者(后者定义更规范),RN仅支持start ,end ,center ,stretch | String | start/top , center/middle , end/bottom , baseline , stretch | center |
alignContent | Flex有多根轴线时的对齐方式,一般用不到,RN不支持 | String | start , end , center , between , around , stretch | stretch |
Flex.Item
Flex.Item组件默认加上了样式flex:1
,保证所有item平均分宽度,Flex容器的children不一定是Flex.Item