react-native之flex布局总结

前端之家收集整理的这篇文章主要介绍了react-native之flex布局总结前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  • flex布局之横纵布局
 <View style={{flex: 1,flexDirection: 'row'}}> <View style={{width: 50,height: 50,backgroundColor: 'powderblue'}} /> <View style={{width: 50,backgroundColor: 'skyblue'}} /> <View style={{width: 50,backgroundColor: 'steelblue'}} /> </View>@H_301_85@ 

当 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,backgroundColor: 'steelblue'}} /> </View>@H_301_85@ 

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

 <View style={{ flex: 1,flexDirection: 'column',justifyContent: 'center',alignItems: 'center',backgroundColor: 'steelblue'}} /> </View>@H_301_85@ 

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

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

原文链接:https://www.f2er.com/react/304177.html

猜你在找的React相关文章