[React Native混合开发]React Native中的Flexbox布局

一 摘要

今天来介绍一下React Native中的布局,在React Native混合开发中采用的布局主要是Flexbox,没有接触过HTML5,对这些布局可能非常陌生.
今天给大家总结一下,首先我们还是来看看它的弹性布局概述.

二 基本原理

CSS Flexible Box模块中定义的弹性盒布局技术,可以根据屏幕尺寸或浏览器窗口尺寸自动调整页面中各局部区域的显示方式,
即实现非常灵活的布局处理。

我们来看一下别人画的一个原理图
这里写图片描述
容器上有 主轴和纵轴的概念,默认主轴(main-axis)是横向,从左到右,纵轴是竖向,从上到下。其中所有的孩子的布局都会受到这两个轴的影响。后面会讲到,有很多相关的css属性就是通过改变主轴和纵轴的方向来实现不同的布局效果的。

弹性盒布局的指定方法为:对需要布局的元素的容器元素使用display:flex;样式属性。
该容器元素中的每一个元素均被称为“Flex item”,将该容器元素称为“Flex container”。

可以通过flex-direction样式属性的使用来控制容器中所有子元素的排列方向,可指定值如下所示。

row:横向排列(默认值)
row-reverse:横向反向排列
column:纵向排列
column-reverse:纵向反向排列

React Native中的Flexbox的工作原理和web上的CSS基本一致,当然也存在少许差异。首先是默认值不同:flexDirection的默认值是column而不是rowalignItems的默认值是stretch而不是flex-start,以及flex只能指定一个数字值。
下面我们来看看 各自布局的特点.

三 Flex Direction

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

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

class shoppApp extends Component {
  render() {
    return (
      // 尝试把`flexDirection`改为`column`看看
      <View style={{flex: 1, flexDirection: 'row'}}>
        <View style={styles.pic1} />
        <View style={styles.pic2} />
        <View style={styles.pic3} />
        <View style={styles.pic2} />
      </View>
    );
  }
};
//具体演示
const styles = StyleSheet.create({
    pic1:{
    width: 100, 
    height: 200, 
    backgroundColor: 'red'
    },
    pic2:{
    width: 100, 
    height: 200, 
    backgroundColor: 'blue'
    },
    pic3:{
    width: 100, 
    height: 200, 
    backgroundColor: 'red'
    },
});

//注册
AppRegistry.registerComponent('shoppApp', () => shoppApp);

效果图
这里写图片描述

四 Justify Content

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

//引入框架
import React, { Component } from 'react';
import { AppRegistry, StyleSheet,View } from 'react-native';

class shoppApp extends Component {
  render() {
    return (
      // 尝试把`justifyContent`改为`center`看看
      // 尝试把`flexDirection`改为`row`看看
      <View style={{
        flex: 1,
        flexDirection: 'column',
        justifyContent: 'space-between',
      }}>
        <View style={styles.pic1} />
        <View style={styles.pic2} />
        <View style={styles.pic3} />
      </View>
    );
  }
};

//具体样式
const styles = StyleSheet.create({
    pic1:{
    width: 100, 
    height: 200, 
    backgroundColor: 'red'
    },
    pic2:{
    width: 100, 
    height: 200, 
    backgroundColor: 'blue'
    },
    pic3:{
    width: 100, 
    height: 200, 
    backgroundColor: 'red'
    },
});

//注册
AppRegistry.registerComponent('shoppApp', () => shoppApp);

效果展示

这里写图片描述

五 Align Items

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

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

框架导入
import React, {
    Component 
} from 'react';
import { 
    AppRegistry, 
    View,
    StyleSheet
} from 'react-native';
定义shopApp类
class shoppApp extends Component {
  render() {
    return (
      // 尝试把`alignItems`改为`flex-start`看看
      // 尝试把`justifyContent`改为`flex-end`看看
      // 尝试把`flexDirection`改为`row`看看
      <View style={{
        flex: 1,
        flexDirection: 'column',
        justifyContent: 'center',
        alignItems: 'center',
      }}>
        <View style={styles.pic1} />
        <View style={styles.pic2} />
        <View style={styles.pic3} />
      </View>
    );
  }
};
样式列表
const styles = StyleSheet.create({
    pic1:{
    width: 100, 
    height: 200, 
    backgroundColor: 'red'
    },
    pic2:{
    width: 100, 
    height: 200, 
    backgroundColor: 'blue'
    },
    pic3:{
    width: 100, 
    height: 200, 
    backgroundColor: 'red'
    },
});

注册

AppRegistry.registerComponent('shoppApp', () => shoppApp);

运行效果

这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

图解AI

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值