React Navtive 的 FlexBox布局

2019-07-31  本文已影响0人  打碟的DJ

React Native中的FlexBox 和Web CSSS上FlexBox的不同之处

RN 中支持的属性

以下属性是React Native所支持的Flex属性

'row', 'column'(默认),'row-reverse','column-reverse‘
'wrap', 'nowrap'(默认)
'flex-start'(默认),'flex-end','center', 'space-between', 'space-around'
'flex-start', 'flex-end', 'center', 'stretch'(默认)

主轴和侧轴

flexDirection

Usage
/**
 * @author 打碟的DJ
 * @date 2019-07-30
 * @desc
 * @flow
 */

import React, { Component } from 'react';
import { View, Text, SafeAreaView } from 'react-native';

export default class FlexBox extends Component{
    render() {
        return(
            <SafeAreaView>
                <View style={{backgroundColor: 'grey', flexDirection: 'row', marginTop: 100}}>
                    <View style={{width: 40, height: 40, backgroundColor: 'green', justifyContent: 'center', margin:5}}>
                        <Text style={{textAlign: 'center', justifyContent: 'center'}}>1</Text>
                    </View>
                    <View style={{width: 40, height: 40, backgroundColor: 'green', justifyContent: 'center', margin:5}}>
                        <Text style={{textAlign: 'center', justifyContent: 'center'}}>2</Text>
                    </View>
                    <View style={{width: 40, height: 40, backgroundColor: 'green', justifyContent: 'center', margin:5}}>
                        <Text style={{textAlign: 'center', justifyContent: 'center'}}>3</Text>
                    </View>
                    <View style={{width: 40, height: 40, backgroundColor: 'green', justifyContent: 'center', margin:5}}>
                        <Text style={{textAlign: 'center', justifyContent: 'center'}}>4</Text>
                    </View>
                    <View style={{width: 40, height: 40, backgroundColor: 'green', justifyContent: 'center', margin:5}}>
                        <Text style={{textAlign: 'center', justifyContent: 'center'}}>5</Text>
                    </View>
                </View>
            </SafeAreaView>
        );
    }
}

flexWrap

const WIDTH = Dimensions.get('window').width;
<View style={{width: WIDTH, backgroundColor: 'grey', flexDirection: 'row', marginTop: 80, flexWrap:'nowrap'}}>
    ...
</View>
<View style={{width: WIDTH,backgroundColor: 'grey', flexDirection: 'row', marginTop: 80, flexWrap:'wrap'}}>
   ...
</View>

justifyContent

justifyContent 属性定义了容器如何顺着主轴的弹性(flex)元素之间及其周围的控件,默认为 flex-start

<View style={{width: WIDTH, backgroundColor: 'grey', flexDirection: 'row', marginTop: 80, justifyContent: 'flex-start'}}>
        ***
</View>

alignItems

alignItems 属性以与 justifyContent 相同的方式在侧轴方向上将当前行上的弹性元素对齐,默认为stretch。

<View style={{width: WIDTH, backgroundColor: 'grey', flexDirection: 'column', margin: 40, alignItems: 'flex-start'}}>
    ***
</View>

子视图属性

alignSelf

'auto', 'flex-start', 'flex-end', 'center', 'stretch'

alignSelf 属性以属性定义了flex容器内被选中项目的对齐方式。注意:alignSelf 属性可重写灵活容器的 alignItems 属性。

<View style={{width: WIDTH, backgroundColor: 'grey', flexDirection: 'column', margin: 40, alignItems: 'center'}}>
    <View style={{width: 80, height: 40, backgroundColor: 'green', justifyContent: 'center', margin:5, alignSelf: 'auto'}}>
        <Text style={{textAlign: 'center', justifyContent: 'center'}}>1</Text>
    </View>
</View>

flex number

flex 属性定义了一个可伸缩元素的能力,根据比例来指定容器大小,默认为0。

<View style={ {width: WIDTH,flexDirection:'row',height:40, backgroundColor:"grey",marginTop:100}}>
    <View style={ {flex:1,backgroundColor:"green",margin:5}}>
        <Text style={ {fontSize:16}}>flex:1</Text>
    </View>
    <View style={ {flex:2,backgroundColor:"green",margin:5}}>
        <Text style={ {fontSize:16}}>flex:2</Text>
    </View>
    <View style={ {flex:3,backgroundColor:"green",margin:5}}>
        <Text style={ {fontSize:16}}>flex:3</Text>
    </View>
</View>
上一篇 下一篇

猜你喜欢

热点阅读