Flex Flex布局

Flex是Flexible Box的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性。

规则

代码演示

基本

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

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

排列方向

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

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

换行

wrap="nowrap/wrap/wrap-reverse"

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

主轴对齐方式

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

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

交叉轴对齐方式

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

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

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
成员说明类型可选值默认值