Flex Flex布局
Flex是Flexible Box的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性。
规则
代码演示
基本
基本,子元素FLex.Item,设置flex=1
import { Flex, Button, WingBlank, WhiteSpace } from 'antd-mobile';
const FlexExample = React.createClass({
render() {
return (
<div className="flex-container">
<WhiteSpace size="lg" />
<WingBlank size="lg">
<Flex>
<Flex.Item>
<Button type="primary">2列</Button>
</Flex.Item>
<Flex.Item>
<Button>2列</Button>
</Flex.Item>
</Flex>
</WingBlank>
<WhiteSpace size="lg" />
<WingBlank size="lg">
<Flex>
<Flex.Item>
<Button type="primary">3列</Button>
</Flex.Item>
<Flex.Item>
<Button>3列</Button>
</Flex.Item>
<Flex.Item>
<Button type="primary">3列</Button>
</Flex.Item>
</Flex>
</WingBlank>
<WhiteSpace size="lg" />
<WingBlank size="lg">
<Flex>
<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.Item>
<Button>4列</Button>
</Flex.Item>
</Flex>
</WingBlank>
<WhiteSpace size="lg" />
<WingBlank size="lg">
<Flex>
<Flex.Item>
<Button type="primary">5列</Button>
</Flex.Item>
<Flex.Item>
<Button>5列</Button>
</Flex.Item>
<Flex.Item>
<Button type="primary">5列</Button>
</Flex.Item>
<Flex.Item>
<Button>5列</Button>
</Flex.Item>
<Flex.Item>
<Button type="primary">5列</Button>
</Flex.Item>
</Flex>
</WingBlank>
<WhiteSpace size="lg" />
</div>
);
},
});
ReactDOM.render(<FlexExample />, mountNode);
.flex-container {
background: #f5f5f9;
}
排列方向
direction="row/row-reverse/column/column-reverse"
import { Flex, List, Button, WhiteSpace, WingBlank } from 'antd-mobile';
const FlexExample = React.createClass({
render() {
return (
<div className="flex-container">
<WhiteSpace />
<List title="row:主轴为水平方向,起点在左端" />
<WingBlank size="lg">
<Flex className="flex-direction-container">
<Button type="primary">按钮1</Button>
<Button>按钮2</Button>
<Button type="primary">按钮3</Button>
<Button>按钮4</Button>
</Flex>
</WingBlank>
<List>
<List.Header>row-reverse:主轴为水平方向,起点在右端</List.Header>
</List>
<WingBlank size="lg">
<Flex
direction="row-reverse"
className="flex-direction-container-reverse"
>
<Button type="primary">按钮1</Button>
<Button>按钮2</Button>
<Button type="primary">按钮3</Button>
<Button>按钮4</Button>
</Flex>
</WingBlank>
<List>
<List.Header>column:主轴为垂直方向,起点在上沿</List.Header>
</List>
<WingBlank size="lg">
<Flex
direction="column"
>
<Button type="primary" style={{ margin: '16px' }}>按钮1</Button>
<Button style={{ margin: '16px' }}>按钮2</Button>
<Button type="primary" style={{ margin: '16px' }}>按钮3</Button>
<Button style={{ margin: '16px' }}>按钮4</Button>
</Flex>
</WingBlank>
<List>
<List.Header>column-reverse:主轴为垂直方向,起点在下沿</List.Header>
</List>
<WingBlank size="lg">
<Flex direction="column-reverse">
<Button type="primary" style={{ margin: '16px' }}>按钮1</Button>
<Button type="primary" style={{ margin: '16px' }}>按钮2</Button>
<Button type="primary" style={{ margin: '16px' }}>按钮3</Button>
<Button type="primary" style={{ margin: '16px' }}>按钮4</Button>
</Flex>
</WingBlank>
<WhiteSpace />
</div>
);
},
});
ReactDOM.render(<FlexExample />, mountNode);
.flex-container {
background: #f5f5f9;
}
.flex-direction-container button {
margin-right: 0.28rem;
}
.flex-direction-container button:last-child {
margin-right: 0;
}
.flex-direction-container-reverse button {
margin-right: 0.28rem;
}
.flex-direction-container-reverse button:first-child {
margin-right: 0;
}
换行
wrap="nowrap/wrap/wrap-reverse"
import { Flex, List, Button, WhiteSpace, WingBlank } from 'antd-mobile';
const FlexExample = React.createClass({
render() {
return (
<div className="flex-container">
<WhiteSpace />
<List>
<List.Header>nowrap(默认):不换行</List.Header>
</List>
<WingBlank size="lg">
<Flex className="flex-button-container">
<Button type="primary">1</Button>
<Button>2</Button>
<Button type="primary">3</Button>
<Button>4</Button>
<Button type="primary">5</Button>
<Button>6</Button>
<Button type="primary">7</Button>
<Button>8</Button>
<Button type="primary">9</Button>
<Button>10</Button>
</Flex>
</WingBlank>
<List>
<List.Header>wrap:换行,第一行在上方</List.Header>
</List>
<WingBlank size="lg">
<Flex
wrap="wrap"
className="flex-button-container"
>
<Button type="primary">1</Button>
<Button>2</Button>
<Button type="primary">3</Button>
<Button>4</Button>
<Button type="primary">5</Button>
<Button>6</Button>
<Button type="primary">7</Button>
<Button>8</Button>
<Button type="primary">9</Button>
<Button>10</Button>
</Flex>
</WingBlank>
<List>
<List.Header>wrap-reverse:换行,第一行在下方</List.Header>
</List>
<WingBlank size="lg">
<Flex
wrap="wrap-reverse"
className="flex-button-container"
>
<Button type="primary">1</Button>
<Button>2</Button>
<Button type="primary">3</Button>
<Button>4</Button>
<Button type="primary">5</Button>
<Button>6</Button>
<Button type="primary">7</Button>
<Button>8</Button>
<Button type="primary">9</Button>
<Button>10</Button>
</Flex>
</WingBlank>
</div>
);
},
});
ReactDOM.render(<FlexExample />, mountNode);
.flex-container {
background: #f5f5f9;
}
.flex-button-container button {
margin-right: 0.18rem;
margin-bottom: 0.18rem;
width: 1.1rem;
}
.flex-button-container button:last-child {
margin-right: 0;
}
主轴对齐方式
justify="start/end/center/between/around"
import { Flex, List, Button, WhiteSpace, WingBlank } from 'antd-mobile';
const FlexExample = React.createClass({
render() {
return (
<div className="flex-container">
<WhiteSpace />
<List title="start(默认): 左对齐" />
<WingBlank size="lg">
<Flex
className="flex-container-justify"
>
<Button size="small" type="primary">按钮1</Button>
<Button size="small">按钮2</Button>
<Button size="small" type="primary">按钮3</Button>
</Flex>
</WingBlank>
<List>
<List.Header>end:右对齐</List.Header>
</List>
<WingBlank size="lg">
<Flex
justify="end"
className="flex-container-justify"
>
<Button size="small" type="primary">按钮1</Button>
<Button size="small">按钮2</Button>
<Button size="small" type="primary">按钮3</Button>
</Flex>
</WingBlank>
<List>
<List.Header>center:居中</List.Header>
</List>
<WingBlank size="lg">
<Flex
justify="center"
className="flex-container-justify"
>
<Button size="small" type="primary">按钮1</Button>
<Button size="small">按钮2</Button>
<Button size="small" type="primary">按钮3</Button>
</Flex>
</WingBlank>
<List>
<List.Header>between:两端对齐,项目之间的间隔都相等</List.Header>
</List>
<WingBlank size="lg">
<Flex
justify="between"
className="flex-container-justify"
>
<Button size="small" type="primary">按钮1</Button>
<Button size="small">按钮2</Button>
<Button size="small" type="primary">按钮3</Button>
</Flex>
</WingBlank>
<List>
<List.Header>around:每个项目两侧的间隔相等。所以,项目之间的间隔比项目与边框的间隔大一倍。</List.Header>
</List>
<WingBlank size="lg">
<Flex
justify="around"
className="flex-container-justify"
>
<Button size="small" type="primary">按钮1</Button>
<Button size="small">按钮2</Button>
<Button size="small" type="primary">按钮3</Button>
</Flex>
</WingBlank>
</div>
);
},
});
ReactDOM.render(<FlexExample />, mountNode);
.flex-container {
background: #f5f5f9;
}
.flex-container-justify button {
margin-right: 0.18rem;
margin-bottom: 0.18rem;
width: 1.4rem;
}
.flex-container-justify button:last-child {
margin-right: 0;
}
交叉轴对齐方式
align="start/end/center/baseline/stretch"
import { Flex, Button, List, WingBlank, WhiteSpace } from 'antd-mobile';
const FlexExample = React.createClass({
render() {
return (
<div className="flex-container">
<WhiteSpace />
<WingBlank size="lg">
<List title="start:交叉轴的起点对齐" />
<Flex align="start" className="flex-align-container">
<Flex.Item>
<Button type="primary">3列</Button>
</Flex.Item>
<Flex.Item>
<Button inline size="small">3列</Button>
</Flex.Item>
</Flex>
</WingBlank>
<WhiteSpace />
<WingBlank size="lg">
<List>
<List.Header>end:交叉轴的终点对齐</List.Header>
</List>
<Flex align="end" className="flex-align-container">
<Flex.Item>
<Button type="primary">3列</Button>
</Flex.Item>
<Flex.Item>
<Button inline size="small">3列</Button>
</Flex.Item>
</Flex>
</WingBlank>
<WhiteSpace />
<WingBlank size="lg">
<List>
<List.Header>center(默认):交叉轴的中点对齐</List.Header>
</List>
<Flex align="center" className="flex-align-container">
<Flex.Item>
<Button type="primary">3列</Button>
</Flex.Item>
<Flex.Item>
<Button inline size="small">3列</Button>
</Flex.Item>
<Flex.Item>
<Button type="primary">3列</Button>
</Flex.Item>
<Flex.Item>
<Button inline size="small">3列</Button>
</Flex.Item>
</Flex>
</WingBlank>
<WhiteSpace />
<WingBlank size="lg">
<List>
<List.Header>baseline:项目的第一行文字的基线对齐</List.Header>
</List>
<Flex align="baseline" className="flex-align-container">
<Flex.Item>
<Button type="primary">3列</Button>
</Flex.Item>
<Flex.Item>
<Button inline size="small">3列</Button>
</Flex.Item>
<Flex.Item>
<Button type="primary">3列</Button>
</Flex.Item>
<Flex.Item>
<Button inline size="small">3列</Button>
</Flex.Item>
</Flex>
</WingBlank>
<WhiteSpace />
<WingBlank size="lg">
<List>
<List.Header>stretch:如果项目未设置高度或设为auto,将占满整个容器的高度</List.Header>
</List>
<Flex align="stretch" className="flex-align-container" style={{ border: '1px solid red' }}>
<Flex.Item>
<Button type="primary">3列</Button>
</Flex.Item>
<Flex.Item>
<Button inline size="small">3列</Button>
</Flex.Item>
<Flex.Item>
<Button type="primary">3列</Button>
</Flex.Item>
<Flex.Item>
<Button inline size="small">3列</Button>
</Flex.Item>
</Flex>
</WingBlank>
<WhiteSpace />
</div>
);
},
});
ReactDOM.render(<FlexExample />, mountNode);
.flex-container {
background: #f5f5f9;
}
.flex-align-container button {
width: 100%;
}
.flex-align-container * {
box-sizing: border-box;
}
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