美文网首页
RN(Flexbox)与Android对比

RN(Flexbox)与Android对比

作者: 孤独的色狼 | 来源:发表于2017-10-24 14:15 被阅读0次

Flexbox主要用flexDirection、alignItems和 justifyContent这三种布局

flexDirection

相当于Android中的orientation
flexDirection可以决定布局的主轴。子元素是应该沿着水平轴(row)方向排列,还是沿着竖直轴(column)方向排列呢?默认值是竖直轴(column)方向

flexDirection(RN) orientation(Android)
row horizontal
column vertical

Justify Content

相当于Android中的layout_gravity
对应的这些可选项有:flex-start、center、flex-end、space-around以及space-between

RN Android
flex-start left
flex-start right
center center
space-between -
space-around -

space-between:占用真个屏幕,空余部分留到中间

    <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 style={{width: 50, height: 50, backgroundColor: '#ffffff'}} />
    </View>
图片.png

space-around:占用整个屏幕,平均分配

    <View style={{flex: 1, flexDirection: 'row',justifyContent: ’space-around',}}>
        <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={{width: 50, height: 50, backgroundColor: '#ffffff'}} />
    </View>
图片.png

Align Items

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

flex-start、center、flex-end不做介绍,主要介绍下stretch

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

 <View style={{flex: 1, flexDirection: 'row',justifyContent: 'space-around',alignItems:'stretch'}}>
        <View style={{width: 50, height: 50, backgroundColor: 'powderblue'}} />
        <View style={{width: 50, height: 50,height: 50, backgroundColor: 'skyblue'}} />
        <View style={{width: 50,height: 50,  backgroundColor: 'steelblue'}} />
        <View style={{width: 50,  backgroundColor: '#ffffff'}} />
      </View>

效果图


图片.png

相关文章

网友评论

      本文标题:RN(Flexbox)与Android对比

      本文链接:https://www.haomeiwen.com/subject/vtutpxtx.html