FlexBox总结

FlexBox弹性布局是React Native中的布局,功能丰富,能够满足页面大量的排版需求。RN的FlexBox和Html5的差别不大,原理基本相同。

它拥有三个属性 flexDirectionjustifyContentalignItems

flexDirection

flexDirection是确定子元素的主轴方向,是沿着水平轴还是垂直轴排列,默认值是column。

有四个属性值:

  • row:水平从左往右
  • row-reverse:水平从右往左
  • column:垂直从上往下
  • column-reverse:垂直从下往上。
import React, { Component } from 'react';
import { AppRegistry, View } from 'react-native';

export default class FlexDirectionBasics extends Component {
  render() {
    return (
      // 尝试把`flexDirection`改为`column`看看
      <View style={{flex: 1, flexDirection: 'row'}}>
        <View style={{width: 50, height: 50, backgroundColor: 'red'}} />
        <View style={{width: 50, height: 50, backgroundColor: 'green'}} />
        <View style={{width: 50, height: 50, backgroundColor: 'blue'}} />
      </View>
    );
  }
};

justifyContent

justifyContent是确定子元素在主轴上的对齐方式

有下面几个属性值:

  • flex-start:沿着主轴确定的方向,起始点对齐排列(默认值)
  • center:沿着主轴确定的方向,居中对齐排列
  • flex-end:沿着主轴确定的方向,终点对齐排列
  • space-around:沿着主轴确定的方向,元素均分排列,每个元素周围分配相同的空间
  • space-between:沿着主轴确定的方向,元素均分排列,两端对齐
  • space-evenly:沿着主轴确定的方向,元素均分排列,每个元素之间的间隔相等
<View style={{
    flex: 1,
    flexDirection: 'row',
    justifyContent: 'space-evenly',
}}>
    <View style={{width: 50, height: 50, backgroundColor: 'red'}}/>
    <View style={{width: 50, height: 50, backgroundColor: 'green'}}/>
    <View style={{width: 50, height: 50, backgroundColor: 'blue'}}/>
</View>

alignItems

alignItems是与主轴垂直的轴的对齐方式

有下面几个属性值:

  • flex-start:和垂直轴方向,起点对齐排列
  • flex-end:和垂直轴方向,终点对齐排列
  • center:和垂直轴方向,居中对齐排列
  • baseline:和垂直轴方向,基线对齐
  • stretch:当元素没有设置(宽)高度,或者(宽)高度为auto,会撑满(默认值)
<View style={{
    flex: 1,
    flexDirection: 'row',
    justifyContent: 'flex-start',
    alignItems: "stretch"
}}>
    <View style={{width: 50, height: 200, backgroundColor: 'red'}}/>
    <View style={{width: 50, height: 200, backgroundColor: 'green'}}/>
    <View style={{width: 50, height: 200, backgroundColor: 'blue'}}/>
</View>

说了这麽多,这不是LinearLayout和RelativeLayout的结合体吗

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值