RN(Flexbox)与Android对比

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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容