react-native之flex布局总结

来源:互联网 发布:dota2比赛数据未找到 编辑:程序博客网 时间:2024/05/21 00:50
  • flex布局之横纵布局
      <View style={{flex: 1, flexDirection: 'row'}}>        <View style={{width: 50, height: 50, backgroundColor: 'powderblue'}} />        <View style={{width: 50, height: 50, backgroundColor: 'skyblue'}} />        <View style={{width: 50, height: 50, backgroundColor: 'steelblue'}} />      </View>

当 flexDirection 为 row 的时候,为横向布局 , 当 flexDirection 为 column 的时候,为纵向布局

  • Justify Content
    在组件的style中指定justifyContent可以决定其子元素沿着主轴的排列方式。子元素是应该靠近主轴的起始端还是末尾段分布呢?亦或应该均匀分布?对应的这些可选项有:flex-start、center、flex-end、space-around以及space-between。
    需要注意的是 Justify Content 同 flexDirection=’row’ 配合使用,如:
      <View style={{        flex: 1,        flexDirection: 'row',        justifyContent: 'space-between',      }}>        <View style={{width: 50, height: 50, backgroundColor: 'powderblue'}} />        <View style={{width: 50, height: 50, backgroundColor: 'skyblue'}} />        <View style={{width: 50, height: 50, backgroundColor: 'steelblue'}} />      </View>

而如果此时 flexDirection: ‘column’, 那么在 justifyContent 为 center、flex-end、space-around的时候都会显示异常,由此可见 justifyContent 更适合与 flexDirection=’row’ 配合使用;
而相对的alignItems就更适合同 flexDirection: ‘column’,使用了,如:

      <View style={{        flex: 1,        flexDirection: 'column',        justifyContent: 'center',        alignItems: 'center',      }}>        <View style={{width: 50, height: 50, backgroundColor: 'powderblue'}} />        <View style={{width: 50, height: 50, backgroundColor: 'skyblue'}} />        <View style={{width: 50, height: 50, backgroundColor: 'steelblue'}} />      </View>

在组件的style中指定alignItems可以决定其子元素沿着次轴(与主轴垂直的轴,比如若主轴方向为row,则次轴方向为column)的排列方式。子元素是应该靠近次轴的起始端还是末尾段分布呢?亦或应该均匀分布?对应的这些可选项有:flex-start、center、flex-end以及stretch。

注意:要使stretch选项生效的话,子元素在次轴方向上不能有固定的尺寸。以下面的代码为例:只有将子元素样式中的width: 50去掉之后,alignItems: ‘stretch’才能生效。 stretch在width去掉后会自动拓展延伸。

0 0
原创粉丝点击