ReactNative学习笔记(五)样式&高度宽度&Flexbo

2020-02-12  本文已影响0人  维仔_411d

样式

import React, {Component} from 'react';
import {StyleSheet, Text, View} from 'react-native';
const styles = StyleSheet.create({
    bigBlue:{
        color:'blue',
        fontWeight:"700",
        fontSize:60,
    },
    red:{
        color:"#f60",
        fontSize:30,
    }
});
export default class BlinkApp extends Component {
    render(){
        return (
            <View>
                <Text style={styles.bigBlue}>just blue</Text>
                <Text style={[styles.bigBlue, styles.red]}>blue then red</Text>
                <Text style={[styles.red, styles.bigBlue]}>red then blue</Text>
            </View>
        );
    }
}
颜色、字号的简单样式

高度与宽度

const styles = StyleSheet.create({
    textAlignCenter:{
        alignItems:'center'
    },
    greyBackground:{
        backgroundColor: '#f3F3F3'
    },
    greenBackground:{
        backgroundColor: '#00e789',
    },
    sHeight100:{
        height:100, // 指定宽高
    }

});

<View style={{flex:1}}> // 弹性宽高, 方向的指定与css中一致 flexDirection:column|row
  <View style={[{flex:1,styles.textAlignCenter, styles.greyBackground}]}</View>
  <View style={[styles.sHeight100, styles.textAlignCenter, styles.greenBackground}]}</View>
</View>

使用Flexbox布局

render(){
        return (
            <View style={{flex:1, flexDirection:'column'}}>
                <Text style={{height:30}}>flex-start↓</Text>
                <View style={{flex:1, flexDirection:'row', justifyContent:'flex-start', textAlign:'center'}}>
                    <View style={{width:70, height:70,  backgroundColor:'coral'}}></View>
                    <View style={{width:70, height:70,  backgroundColor:'lightblue'}}></View>
                    <View style={{width:70, height:70,  backgroundColor:'khaki'}}></View>
                    <View style={{width:70, height:70,  backgroundColor:'pink'}}></View>
                </View>
                <Text style={{height:30}}>flex-end↓</Text>
                <View style={{flex:1, flexDirection:'row', justifyContent:'flex-end'}}>
                    <View style={{width:70, height:70,  backgroundColor:'coral'}}></View>
                    <View style={{width:70, height:70,  backgroundColor:'lightblue'}}></View>
                    <View style={{width:70, height:70,  backgroundColor:'khaki'}}></View>
                    <View style={{width:70, height:70,  backgroundColor:'pink'}}></View>
                </View>
                <Text style={{height:30}}>center↓</Text>
                <View style={{flex:1, flexDirection:'row', justifyContent:'center'}}>
                    <View style={{width:70, height:70,  backgroundColor:'coral'}}></View>
                    <View style={{width:70, height:70,  backgroundColor:'lightblue'}}></View>
                    <View style={{width:70, height:70,  backgroundColor:'khaki'}}></View>
                    <View style={{width:70, height:70,  backgroundColor:'pink'}}></View>
                </View>
                <Text style={{height:30}}>space-between↓</Text>
                <View style={{flex:1, flexDirection:'row', justifyContent:'space-between'}}>
                    <View style={{width:70, height:70,  backgroundColor:'coral'}}></View>
                    <View style={{width:70, height:70,  backgroundColor:'lightblue'}}></View>
                    <View style={{width:70, height:70,  backgroundColor:'khaki'}}></View>
                    <View style={{width:70, height:70,  backgroundColor:'pink'}}></View>
                </View>
                <Text style={{height:30}}>space-around↓</Text>
                <View style={{flex:1, flexDirection:'row', justifyContent:'space-around'}}>
                    <View style={{width:70, height:70,  backgroundColor:'coral'}}></View>
                    <View style={{width:70, height:70,  backgroundColor:'lightblue'}}></View>
                    <View style={{width:70, height:70,  backgroundColor:'khaki'}}></View>
                    <View style={{width:70, height:70,  backgroundColor:'pink'}}></View>
                </View>
            </View>
        );
    }
justifyContent效果图
上一篇下一篇

猜你喜欢

热点阅读