React-Native 基础(六) 使用Flexbox布局

参考文档:http://facebook.github.io/react-native/docs/flexbox.html
1. 使用flexbox可以指定子布局
2. flexbox兼容不同尺寸的手机屏幕
3. 通常使用flexDirection, alignItems, 和justifyContent的组合实现目标效果

flexDirection
指定子布局是横向(row)还是纵向(column),默认是纵向

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={{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);

justifyContent
决定子布局沿着主轴的分布,可选项有flex-start,center,flex-end,space-aroundspace-between.

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: '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);

这里写图片描述

alignItems
alignItems决定子布局沿着副轴(如果主轴是纵轴,那么副轴是横轴,反之亦然)的对齐方式。可选项:flex-start, center, flex-end, 和stretch. 使用stretch时要注意:只有当子布局在副轴上没有固定尺寸时,stretch才有效(这玩意儿相当于match-parent)。
这里写图片描述

更多的布局参见 http://facebook.github.io/react-native/docs/layout-props.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值