美文网首页
React Native 学习手记(三)

React Native 学习手记(三)

作者: NiuLinguo | 来源:发表于2019-08-20 17:01 被阅读0次

本章节主要介绍

  • 样式
  • 宽度高度
  • Flexbox布局

样式

  • 所有的组件都接受名为style的属性。
  • 命名方法注意使用驼峰命名方法。
  • style属性可以传入对象,也可以传入对象数组。
  • 数组中,后面的样式对象比前面的优先级高。
  • 使用StyleSheet.create集中定义组件的样式。
import React, { Component } from 'react';
import { AppRegistry, StyleSheet, Text, View } from 'react-native';

export default class LotsOfStyles extends Component {
  render() {
    return (
      <View>
        <Text style={styles.red}>just red</Text>
        <Text style={styles.bigBlue}>just bigBlue</Text>
        <Text style={[styles.bigBlue, styles.red]}>bigBlue, then red</Text>
        <Text style={[styles.red, styles.bigBlue]}>red, then bigBlue</Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  bigBlue: {
    color: 'blue',
    fontWeight: 'bold',
    fontSize: 30,
  },
  red: {
    color: 'red',
  },
});
运行效果

宽度高度

  • 在样式中指定固定的widthheight
  • 无单位,表示逻辑像素点(类似Android中的dp)

弹性宽高

组件样式中,可以使用flex动态的扩张或收缩,类似于Android中的weight。

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

export default class JustifyContentBasics extends Component {
  render() {
    return (
      // 尝试把`justifyContent`改为`center`看看
      // 尝试把`flexDirection`改为`row`看看
      <View style={{
        flex: 1,
        flexDirection: 'column',
        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>
    );
  }
};

Flexbox布局

Flexbox指的是一组样式属性,先看一下简易布局图解大概了解一下。

flexDirection

决定布局的主轴方向,默认是column,可以设置成row或者column

justifyContent

决定子布局主轴方向上的排列方式。

可取值为:

  • flex-start:子布局主轴方向上靠前排列。
  • center:子布局主轴方向上居中排列。
  • flex-end:子布局主轴方向上靠后排列。
  • space-around:子布局主轴方向上平均排列,两边只留半个间距。
  • space-between:子布局主轴方向上平均排列,两边不留间距。
  • space-evenly:子布局主轴方向上平均排列,两边也留一个间距。

为了区分出来这6个属性的区别,结合flexDirection的2个属性,用代码画出12个组合的局部,方便对比其中的区别。

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

class PrintStyleComponent extends Component {
  render() {

    let justifyContent = this.props.justifyContent
    let backgroundColor = this.props.backgroundColor
    let flexDirection = this.props.flexDirection

    return (
      <View style={{flex:1, backgroundColor:backgroundColor, alignItems:"stretch", justifyContent:"center"}}>
        <View style={{flex:1, flexDirection:flexDirection, justifyContent:justifyContent}}>
            <View style={{width:20, height:20, backgroundColor:"red", alignItems:"center", justifyContent:"center"}}><Text>1</Text></View>
            <View style={{width:20, height:20, backgroundColor:"red", alignItems:"center", justifyContent:"center"}}><Text>2</Text></View>
            <View style={{width:20, height:20, backgroundColor:"red", alignItems:"center", justifyContent:"center"}}><Text>3</Text></View>
        </View>
        <View style={{position: "absolute", alignSelf:"center", alignItems:"center"}}>
            <Text style={{color:"white"}}>{justifyContent}</Text>
            <Text style={{color:"white"}}>{flexDirection}</Text>
        </View>
      </View>
    )
  }
}

export default class FlexDimensionsBasics extends Component {
  render() {
    return (
      <View style={{flex:1, flexDirection:"column"}}>
        <View style={{flex:1, flexDirection:"row"}}>
          <PrintStyleComponent justifyContent="flex-start" backgroundColor="steelblue" flexDirection="row"/>
          <PrintStyleComponent justifyContent="center" backgroundColor="skyblue" flexDirection="row"/>
          <PrintStyleComponent justifyContent="flex-end" backgroundColor="powderblue" flexDirection="row"/>
        </View>
        <View style={{flex:1, flexDirection:"row"}}>
          <PrintStyleComponent justifyContent="space-around" backgroundColor="powderblue" flexDirection="row"/>
          <PrintStyleComponent justifyContent="space-between" backgroundColor="steelblue" flexDirection="row"/>
          <PrintStyleComponent justifyContent="space-evenly" backgroundColor="skyblue" flexDirection="row"/>
        </View>
        <View style={{flex:1, flexDirection:"row"}}>
          <PrintStyleComponent justifyContent="flex-start" backgroundColor="steelblue" flexDirection="column"/>
          <PrintStyleComponent justifyContent="center" backgroundColor="skyblue" flexDirection="column"/>
          <PrintStyleComponent justifyContent="flex-end" backgroundColor="powderblue" flexDirection="column"/>
        </View>
        <View style={{flex:1, flexDirection:"row"}}>
          <PrintStyleComponent justifyContent="space-around" backgroundColor="powderblue" flexDirection="column"/>
          <PrintStyleComponent justifyContent="space-between" backgroundColor="steelblue" flexDirection="column"/>
          <PrintStyleComponent justifyContent="space-evenly" backgroundColor="skyblue" flexDirection="column"/>
        </View>
      </View>
    );
  }
}
justifyContent样式效果图

alignItems

决定子布局主轴垂直方向上的排列方式。说的专业点,就是次轴(与主轴垂直的轴)方向上的排列方式。取值有一下四种:

  • flex-start
  • center
  • flex-end
  • stretch

前三个从字面意思就可以知道其意思。最后一个指的是子布局拉长至父布局的宽度。因此,子布局不需要设置宽度。如果设置宽度,stretch将不起作用。

alignSelf

与alignItems的取值一样,效果也一样,不一样的是:

  • alignItems用在父布局中,决定子布局在次轴方向上的排列。
  • alignSelf用在子布局中,决定自己在父布局次轴方向上的排列。

所以如果父布局用alignItems指定了子布局的排列方式,但是某个子布局偏偏想特立独行,那就给子布局加上alignSelf,让他自己决定自己的命运,哦不,是排列方式。

flexWrap、alignContent

这两个属性联合使用,flexWrap取值为wrap,alignContent取值为:

  • flex-start
  • flex-end
  • center
  • stretch
  • space-between
  • space-around
import React, {
  Component
} from 'react';
import {
  AppRegistry,
  View,
  Text
} from 'react-native';

class PrintStyleComponent extends Component {
  render() {

    let alignContent = this.props.alignContent
    let backgroundColor = this.props.backgroundColor;

    return (
      <View style={{justifyContent:"center", alignItems:"stretch", backgroundColor:backgroundColor}}>
          <View style={{flexDirection:"row", flexWrap:"wrap", alignContent:alignContent, height:80}}>
              <View style={{width:100, height:30, backgroundColor:"steelblue"}}></View>
              <View style={{width:100, height:30, backgroundColor:"skyblue"}}></View>
              <View style={{width:100, height:30, backgroundColor:"powderblue"}}></View>
              <View style={{width:100, height:30, backgroundColor:"powderblue"}}></View>
          </View>
          <View style={{position:"absolute", alignSelf:"center"}}>
              <Text style={{fontSize:30}}>{alignContent}</Text>
          </View>
      </View>
    )
  }
}

export default class FlexDimensionsBasics extends Component {
  render() {
    return (
      <View style={{flex:1, flexDirection:"column"}}>
          <PrintStyleComponent alignContent="flex-start" backgroundColor="#eee"/>
          <PrintStyleComponent alignContent="flex-end" backgroundColor="#fff"/>
          <PrintStyleComponent alignContent="center" backgroundColor="#ddd"/>
          <PrintStyleComponent alignContent="stretch" backgroundColor="#eee"/>
          <PrintStyleComponent alignContent="space-between" backgroundColor="#fff"/>
          <PrintStyleComponent alignContent="space-around" backgroundColor="#ddd"/>
      </View>
    );
  }
}
flexWrap、alignContent

相关文章

网友评论

      本文标题:React Native 学习手记(三)

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