Flex Flex布局

定义/Definition

flexbox 布局

规则 / Rule

Flex布局的相关知识请先阅读阮一峰的博客Flex 布局教程## 代码演示

基本

基本,子元素FLex.Item,设置flex=1

  1. import { Flex, Button, WingBlank, WhiteSpace } from 'antd-mobile';
  2. let FlexExample = React.createClass({
  3. render() {
  4. return (
  5. <div className="button-container">
  6. <WhiteSpace />
  7. <WingBlank>
  8. <Flex>
  9. <Flex.Item>
  10. <Button>2列</Button>
  11. </Flex.Item>
  12. <Flex.Item>
  13. <Button type="primary">2列</Button>
  14. </Flex.Item>
  15. </Flex>
  16. </WingBlank>
  17. <WhiteSpace />
  18. <WingBlank>
  19. <Flex>
  20. <Flex.Item>
  21. <Button size="small">3列</Button>
  22. </Flex.Item>
  23. <Flex.Item>
  24. <Button type="primary" size="small">3列</Button>
  25. </Flex.Item>
  26. <Flex.Item>
  27. <Button size="small">3列</Button>
  28. </Flex.Item>
  29. </Flex>
  30. </WingBlank>
  31. <WhiteSpace />
  32. <WingBlank>
  33. <Flex>
  34. <Flex.Item>
  35. <Button>4列</Button>
  36. </Flex.Item>
  37. <Flex.Item>
  38. <Button type="primary">4列</Button>
  39. </Flex.Item>
  40. <Flex.Item>
  41. <Button>4列</Button>
  42. </Flex.Item>
  43. <Flex.Item>
  44. <Button type="primary">4列</Button>
  45. </Flex.Item>
  46. </Flex>
  47. </WingBlank>
  48. <WhiteSpace />
  49. <WingBlank>
  50. <Flex>
  51. <Flex.Item>
  52. <Button size="small">5列</Button>
  53. </Flex.Item>
  54. <Flex.Item>
  55. <Button type="primary">5列</Button>
  56. </Flex.Item>
  57. <Flex.Item>
  58. <Button size="small">5列</Button>
  59. </Flex.Item>
  60. <Flex.Item>
  61. <Button type="primary">5列</Button>
  62. </Flex.Item>
  63. <Flex.Item>
  64. <Button type="primary">5列</Button>
  65. </Flex.Item>
  66. </Flex>
  67. </WingBlank>
  68. <WhiteSpace />
  69. </div>
  70. );
  71. },
  72. });
  73. ReactDOM.render(<FlexExample />, mountNode);

排列方向

direction="row/row-reverse/column/column-reverse"

  1. import { Flex, List, Button, WhiteSpace } from 'antd-mobile';
  2. let FlexExample = React.createClass({
  3. render() {
  4. return (
  5. <div className="button-container">
  6. <WhiteSpace />
  7. <List>
  8. <List.Header>row:主轴为水平方向,起点在左端</List.Header>
  9. </List>
  10. <Flex>
  11. <Button size="small" type="primary" style={{ margin: '2px' }}>按钮1</Button>
  12. <Button size="small" style={{ margin: '2px' }}>按钮2</Button>
  13. <Button size="small" type="primary" style={{ margin: '2px' }}>按钮3</Button>
  14. <Button size="small" style={{ margin: '2px' }}>按钮4</Button>
  15. </Flex>
  16. <List>
  17. <List.Header>row-reverse:主轴为水平方向,起点在右端</List.Header>
  18. </List>
  19. <Flex
  20. direction="row-reverse"
  21. >
  22. <Button size="small" type="primary" style={{ margin: '2px' }}>按钮1</Button>
  23. <Button size="small" style={{ margin: '2px' }}>按钮2</Button>
  24. <Button size="small" type="primary" style={{ margin: '2px' }}>按钮3</Button>
  25. <Button size="small" style={{ margin: '2px' }}>按钮4</Button>
  26. </Flex>
  27. <List>
  28. <List.Header>column:主轴为垂直方向,起点在上沿</List.Header>
  29. </List>
  30. <Flex
  31. direction="column"
  32. >
  33. <Button size="small" type="primary" style={{ margin: '2px' }}>按钮1</Button>
  34. <Button size="small" style={{ margin: '2px' }}>按钮2</Button>
  35. <Button size="small" type="primary" style={{ margin: '2px' }}>按钮3</Button>
  36. <Button size="small" style={{ margin: '2px' }}>按钮4</Button>
  37. </Flex>
  38. <List>
  39. <List.Header>column-reverse:主轴为垂直方向,起点在下沿</List.Header>
  40. </List>
  41. <Flex
  42. direction="column-reverse"
  43. >
  44. <Button size="small" type="primary" style={{ margin: '2px' }}>按钮1</Button>
  45. <Button size="small" style={{ margin: '2px' }}>按钮2</Button>
  46. <Button size="small" type="primary" style={{ margin: '2px' }}>按钮3</Button>
  47. <Button size="small" style={{ margin: '2px' }}>按钮4</Button>
  48. </Flex>
  49. <WhiteSpace />
  50. </div>
  51. );
  52. },
  53. });
  54. ReactDOM.render(<FlexExample />, mountNode);

换行

wrap="nowrap/wrap/wrap-reverse"

  1. import { Flex, List, Button, WhiteSpace } from 'antd-mobile';
  2. let FlexExample = React.createClass({
  3. render() {
  4. return (
  5. <div className="button-container">
  6. <WhiteSpace />
  7. <List>
  8. <List.Header>nowrap(默认):不换行</List.Header>
  9. </List>
  10. <Flex>
  11. <Button size="small" type="primary" style={{ margin: '2px', width: '40px' }}>1</Button>
  12. <Button size="small" style={{ margin: '2px', width: '40px' }}>2</Button>
  13. <Button size="small" type="primary" style={{ margin: '2px', width: '40px' }}>3</Button>
  14. <Button size="small" style={{ margin: '2px', width: '40px' }}>4</Button>
  15. <Button size="small" type="primary" style={{ margin: '2px', width: '40px' }}>5</Button>
  16. <Button size="small" style={{ margin: '2px', width: '40px' }}>6</Button>
  17. <Button size="small" type="primary" style={{ margin: '2px', width: '40px' }}>7</Button>
  18. <Button size="small" style={{ margin: '2px', width: '40px' }}>8</Button>
  19. <Button size="small" type="primary" style={{ margin: '2px', width: '40px' }}>9</Button>
  20. <Button size="small" style={{ margin: '2px', width: '40px' }}>10</Button>
  21. <Button size="small" type="primary" style={{ margin: '2px', width: '40px' }}>11</Button>
  22. <Button size="small" style={{ margin: '2px', width: '40px' }}>12</Button>
  23. </Flex>
  24. <List>
  25. <List.Header>wrap:换行,第一行在上方</List.Header>
  26. </List>
  27. <Flex
  28. wrap="wrap"
  29. >
  30. <Button size="small" type="primary" style={{ margin: '2px', width: '40px' }}>1</Button>
  31. <Button size="small" style={{ margin: '2px', width: '40px' }}>2</Button>
  32. <Button size="small" type="primary" style={{ margin: '2px', width: '40px' }}>3</Button>
  33. <Button size="small" style={{ margin: '2px', width: '40px' }}>4</Button>
  34. <Button size="small" type="primary" style={{ margin: '2px', width: '40px' }}>5</Button>
  35. <Button size="small" style={{ margin: '2px', width: '40px' }}>6</Button>
  36. <Button size="small" type="primary" style={{ margin: '2px', width: '40px' }}>7</Button>
  37. <Button size="small" style={{ margin: '2px', width: '40px' }}>8</Button>
  38. <Button size="small" type="primary" style={{ margin: '2px', width: '40px' }}>9</Button>
  39. <Button size="small" style={{ margin: '2px', width: '40px' }}>10</Button>
  40. <Button size="small" type="primary" style={{ margin: '2px', width: '40px' }}>11</Button>
  41. <Button size="small" style={{ margin: '2px', width: '40px' }}>12</Button>
  42. </Flex>
  43. <List>
  44. <List.Header>wrap-reverse:换行,第一行在下方</List.Header>
  45. </List>
  46. <Flex
  47. wrap="wrap-reverse"
  48. >
  49. <Button size="small" type="primary" style={{ margin: '2px', width: '40px' }}>1</Button>
  50. <Button size="small" style={{ margin: '2px', width: '40px' }}>2</Button>
  51. <Button size="small" type="primary" style={{ margin: '2px', width: '40px' }}>3</Button>
  52. <Button size="small" style={{ margin: '2px', width: '40px' }}>4</Button>
  53. <Button size="small" type="primary" style={{ margin: '2px', width: '40px' }}>5</Button>
  54. <Button size="small" style={{ margin: '2px', width: '40px' }}>6</Button>
  55. <Button size="small" type="primary" style={{ margin: '2px', width: '40px' }}>7</Button>
  56. <Button size="small" style={{ margin: '2px', width: '40px' }}>8</Button>
  57. <Button size="small" type="primary" style={{ margin: '2px', width: '40px' }}>9</Button>
  58. <Button size="small" style={{ margin: '2px', width: '40px' }}>10</Button>
  59. <Button size="small" type="primary" style={{ margin: '2px', width: '40px' }}>11</Button>
  60. <Button size="small" style={{ margin: '2px', width: '40px' }}>12</Button>
  61. </Flex>
  62. </div>
  63. );
  64. },
  65. });
  66. ReactDOM.render(<FlexExample />, mountNode);

主轴对齐方式

justify="start/end/center/between/around"

  1. import { Flex, List, Button, WhiteSpace } from 'antd-mobile';
  2. let FlexExample = React.createClass({
  3. render() {
  4. return (
  5. <div className="button-container">
  6. <WhiteSpace />
  7. <List>
  8. <List.Header>start(默认): 左对齐</List.Header>
  9. </List>
  10. <Flex>
  11. <Button size="small" type="primary" style={{ width: '70px', margin: '2px' }}>按钮1</Button>
  12. <Button size="small" style={{ width: '70px', margin: '2px' }}>按钮2</Button>
  13. <Button size="small" type="primary" style={{ width: '70px', margin: '2px' }}>按钮3</Button>
  14. </Flex>
  15. <List>
  16. <List.Header>end:右对齐</List.Header>
  17. </List>
  18. <Flex
  19. justify="end"
  20. >
  21. <Button size="small" type="primary" style={{ width: '70px', margin: '2px' }}>按钮1</Button>
  22. <Button size="small" style={{ width: '70px', margin: '2px' }}>按钮2</Button>
  23. <Button size="small" type="primary" style={{ width: '70px', margin: '2px' }}>按钮3</Button>
  24. </Flex>
  25. <List>
  26. <List.Header>center:居中</List.Header>
  27. </List>
  28. <Flex
  29. justify="center"
  30. >
  31. <Button size="small" type="primary" style={{ width: '70px', margin: '2px' }}>按钮1</Button>
  32. <Button size="small" style={{ width: '70px', margin: '2px' }}>按钮2</Button>
  33. <Button size="small" type="primary" style={{ width: '70px', margin: '2px' }}>按钮3</Button>
  34. </Flex>
  35. <List>
  36. <List.Header>between:两端对齐,项目之间的间隔都相等</List.Header>
  37. </List>
  38. <Flex
  39. justify="between"
  40. >
  41. <Button size="small" type="primary" style={{ width: '70px', margin: '2px' }}>按钮1</Button>
  42. <Button size="small" style={{ width: '70px', margin: '2px' }}>按钮2</Button>
  43. <Button size="small" type="primary" style={{ width: '70px', margin: '2px' }}>按钮3</Button>
  44. </Flex>
  45. <List>
  46. <List.Header>around:每个项目两侧的间隔相等。所以,项目之间的间隔比项目与边框的间隔大一倍。</List.Header>
  47. </List>
  48. <Flex
  49. justify="around"
  50. >
  51. <Button size="small" type="primary" style={{ width: '70px', margin: '2px' }}>按钮1</Button>
  52. <Button size="small" style={{ width: '70px', margin: '2px' }}>按钮2</Button>
  53. <Button size="small" type="primary" style={{ width: '70px', margin: '2px' }}>按钮3</Button>
  54. </Flex>
  55. </div>
  56. );
  57. },
  58. });
  59. ReactDOM.render(<FlexExample />, mountNode);

交叉轴对齐方式

align="start/end/center/baseline/stretch"

  1. import { Flex, Button, List, WingBlank, WhiteSpace } from 'antd-mobile';
  2. let FlexExample = React.createClass({
  3. render() {
  4. return (
  5. <div className="button-container">
  6. <WhiteSpace />
  7. <WingBlank>
  8. <List>
  9. <List.Header>start:交叉轴的起点对齐</List.Header>
  10. </List>
  11. <Flex align="start">
  12. <Flex.Item>
  13. <Button type="primary">3列</Button>
  14. </Flex.Item>
  15. <Flex.Item>
  16. <Button size="small">3列</Button>
  17. </Flex.Item>
  18. </Flex>
  19. </WingBlank>
  20. <WhiteSpace />
  21. <WingBlank>
  22. <List>
  23. <List.Header>end:交叉轴的终点对齐</List.Header>
  24. </List>
  25. <Flex align="end">
  26. <Flex.Item>
  27. <Button type="primary">3列</Button>
  28. </Flex.Item>
  29. <Flex.Item>
  30. <Button size="small">3列</Button>
  31. </Flex.Item>
  32. </Flex>
  33. </WingBlank>
  34. <WhiteSpace />
  35. <WingBlank>
  36. <List>
  37. <List.Header>center(默认):交叉轴的中点对齐</List.Header>
  38. </List>
  39. <Flex align="center">
  40. <Flex.Item>
  41. <Button type="primary">3列</Button>
  42. </Flex.Item>
  43. <Flex.Item>
  44. <Button size="small">3列</Button>
  45. </Flex.Item>
  46. <Flex.Item>
  47. <Button type="primary">3列</Button>
  48. </Flex.Item>
  49. <Flex.Item>
  50. <Button size="small">3列</Button>
  51. </Flex.Item>
  52. </Flex>
  53. </WingBlank>
  54. <WhiteSpace />
  55. <WingBlank>
  56. <List>
  57. <List.Header>baseline:项目的第一行文字的基线对齐</List.Header>
  58. </List>
  59. <Flex align="baseline">
  60. <Flex.Item>
  61. <Button type="primary">3列</Button>
  62. </Flex.Item>
  63. <Flex.Item>
  64. <Button size="small">3列</Button>
  65. </Flex.Item>
  66. <Flex.Item>
  67. <Button type="primary">3列</Button>
  68. </Flex.Item>
  69. <Flex.Item>
  70. <Button size="small">3列</Button>
  71. </Flex.Item>
  72. </Flex>
  73. </WingBlank>
  74. <WhiteSpace />
  75. <WingBlank>
  76. <List>
  77. <List.Header>stretch:如果项目未设置高度或设为auto,将占满整个容器的高度</List.Header>
  78. </List>
  79. <Flex align="stretch">
  80. <Flex.Item style={{ border: '1px solid red' }}>
  81. <Button type="primary">3列</Button>
  82. </Flex.Item>
  83. <Flex.Item style={{ border: '1px solid red' }}>
  84. <Button size="small">3列</Button>
  85. </Flex.Item>
  86. <Flex.Item style={{ border: '1px solid red' }}>
  87. <Button type="primary">3列</Button>
  88. </Flex.Item>
  89. <Flex.Item style={{ border: '1px solid red' }}>
  90. <Button size="small">3列</Button>
  91. </Flex.Item>
  92. </Flex>
  93. </WingBlank>
  94. <WhiteSpace />
  95. </div>
  96. );
  97. },
  98. });
  99. ReactDOM.render(<FlexExample />, mountNode);

FlexFlex布局 - 图1

API

Flex

成员说明类型可选值默认值
directionFlex子元素的排列方向Stringrow(水平方向,起点在左端), row-reverse(水平方向,起点在右端), column(垂直方向,起点在上沿), column-reverse(垂直方向,起点在下沿),RN仅支持row,columnrow
wrapFlex 子元素的换行方式Stringnowrap(不换行), wrap(换行,第一行在上方), wrap-reverse(换行,第一行在下方),RN仅支持nowrap,wrapnowrap
justifyFlex 子元素在主轴上的对齐方式Stringstart, end, center, between, around),RN全部支持start
alignFlex 子元素在交叉轴上的对齐方式, top和start等价,middle和center等价,bottom和end等价, 优先使用后者(后者定义更规范),RN仅支持start,end,center,stretchStringstart/top, center/middle, end/bottom, baseline, stretchcenter
alignContentFlex有多根轴线时的对齐方式,一般用不到,RN不支持Stringstart, end, center, between, around, stretchstretch

Flex.Item

Flex.Item组件默认加上了样式flex:1,保证所有item平均分宽度,Flex容器的children不一定是Flex.Item
成员说明类型可选值默认值