007_ReactNative: Layout with Fle

2016-08-21  本文已影响0人  莫_名

(问渠那得清如许,为有源头活水来。 双手奉上RN官网)

Flexbox: 用于在不同的屏幕上显示相同的布局. 需要使用flexDirection, alignItems, 和 justifyContent 相互组合达到目标布局效果

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

class FlexDirectionBasics extends Component {
  render() {
    return (
      // Try setting `flexDirection` to `column`.
//       <View style={{flex: 1, flexDirection: 'row'}}>
      <View style={{flex: 1}}> {/* 默认column */}
        <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>
    );
  }
};

AppRegistry.registerComponent('AwesomeProject', () => FlexDirectionBasics);

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

class JustifyContentBasics extends Component {
  render() {
    return (
      // Try setting `justifyContent` to `center`.
      // Try setting `flexDirection` to `row`.
      <View style={{
        flex: 1,
        flexDirection: 'column',
//         justifyContent: 'flex-start', //从开始点紧密排列
//         justifyContent: 'center', //整体居中排列
//         justifyContent: 'flex-end', //末尾与结束点对齐
//           justifyContent: 'space-around', //每个元素的外边距相等
          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>
    );
  }
};

AppRegistry.registerComponent('AwesomeProject', () => JustifyContentBasics);


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

class AlignItemsBasics extends Component {
  render() {
    return (
      // Try setting `alignItems` to 'flex-start'
      // Try setting `justifyContent` to `flex-end`.
      // Try setting `flexDirection` to `row`.
      <View style={{
        flex: 1,
        flexDirection: 'column',
        justifyContent: 'center',
        //flex-start, center, flex-end 与justifyContent中效果相同
        //stretch为与父元素相同.但前提条件是子元素在次级轴向上没有指定一个确认的值
//         alignItems: 'flex-end',
      }}>
        {/*这里没有指定确定的宽度,则宽度与父相同*/}
        <View style={{ height: 50, backgroundColor: 'powderblue'}} />
        {/*这里指定确定的宽度,则显示为指定的值*/}
        <View style={{width: 50, height: 50, backgroundColor: 'skyblue'}} />
        <View style={{width: 50, height: 50, backgroundColor: 'steelblue'}} />
      </View>
    );
  }
};

AppRegistry.registerComponent('AwesomeProject', () => AlignItemsBasics);

上一篇 下一篇

猜你喜欢

热点阅读