分享

iOS开发 (RN)ReactNative 中flexbox布局

 玄冰优 2016-08-22

我们在React Native中使用flexbox规则来指定某个组件的子元素的布局。Flexbox可以在不同屏幕尺寸上提供一致的布局结构。

一般来说,使用flexDirectionalignItemsjustifyContent三个样式属性就已经能满足大多数布局需求。

Flex Direction

在组件的style中指定flexDirection可以决定布局的主轴。子元素是应该沿着水平轴(row)方向排列,还是沿着竖直轴(column)方向排列呢?默认值是竖直轴(column)方向。

import React, { Component } from 'react';
import { AppRegistry, View } from 'react-native'; class testRNProOne extends Component {
 render() {
 return (
 // 尝试把`flexDirection`改为`column`看看
 <View style={{flex: 1, flexDirection: 'row'}}>
 <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('testRNProOne', () => testRNProOne);
 
 
 

Justify Content

在组件的style中指定justifyContent可以决定其子元素沿着主轴排列方式。子元素是应该靠近主轴的起始端还是末尾段分布呢?亦或应该均匀分布?对应的这些可选项有:flex-start(起始端)center(中间)flex-end(末尾端)、space-around(两端留白,均分)以及space-between(两端不留白均分)。

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> ); }
 
 
 
 
 
 

Align Items

在组件的style中指定alignItems可以决定其子元素沿着次轴(与主轴垂直的轴,比如若主轴方向为row,则次轴方向为column)的排列方式。子元素是应该靠近次轴的起始端还是末尾段分布呢?亦或应该均匀分布?对应的这些可选项有:flex-startcenterflex-end以及stretch

注意:要使stretch选项生效的话,子元素在次轴方向上不能有固定的尺寸。以下面的代码为例:只有将子元素样式中的width: 50去掉之后,alignItems: 'stretch'才能生效。

render() {
    return (
      <View style={{flex: 1, flexDirection: 'row',justifyContent: 'flex-end',alignItems: 'center'}}>
        <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>
    );
  }




http:///docs/0.31/layout-with-flexbox.html







    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多