React Native - Flex布局讲解

作者: 考特林 | 来源:发表于2017-07-05 22:40 被阅读0次
rn

RN的布局采用的是Flex,弹性盒模型。

有四个主要属性:flex、flexDirection、justifyContent、alignItems。
将对依次讲解。

目录

  1. flex的使用。
  2. flexDirection的取值和使用。
  3. justifyContent的取值和使用。
  4. alignItems的取值和使用。
  5. 他们的结合使用。

1. flex的使用

我们可以经常看到在代码中使用 flex:1 ,那么这是什么意思呢?
可以理解为比重

· 如果同级组件上只有一个,并且设置了 flex:1,那么这个组件相当于分配了全部空间。
· 如果同级组件上只有两个,并且这两个都设置了 flex:1,那么相当于这两个组件平均分配了全部空间。
· 如果同级组件上只有两个,并且第一个组件设置了 flex:1,第二个组件设置了 flex:2,那么相当于第一个组件占据全部空间的三分之一,第二个组件占据全部空间的三分之二。
· 如果没有设置 flex 属性,那么这个组件按需分配空间。

以此类推。

下面给出三个例子:

1.最外层同级只有一个组件,并且设置了flex:1,那么它(粉色)占据全部空间。

export default class demo extends Component {
  render() {
    return (
      <View style={{flex : 1, backgroundColor:"pink"}}>
      </View>
    );
  }
}
flex-one

2.最外层同级只有一个组件,并且设置了flex:1,width设置了一个固定宽度,那么它(粉色)的高度占据全部空间。
(但如果height设置一个固定高度的话,则height失效不起效果,因为组件是竖向排列,此时flex的优先级大于height。)

export default class demo extends Component {
  render() {
    return (
      <View style={{width:60,flex:1,backgroundColor:"pink"}}>
      </View>
    );
  }
}
flex-second

3.最外层同级只有两个组件,并且width设置了一个固定宽度,第一个组件设置了flex:2,第二个组件设置了flex:1,那么
第一个组件(粉色)的高度占据全部空间三分之二。
第二个组件(蓝色)的高度占据全部空间三分之一。

export default class demo extends Component {
  render() {
    return (
      <View style={{width:60,flex:1}}>
        //第一个组件
        <View style={{flex:2,backgroundColor:"pink"}}/>
        //第二个组件
        <View style={{flex:1,backgroundColor:"blue"}}/>
      </View>
    );
  }
}
flex-third.png

2. flexDirection 的取值和使用

取值:
column 竖向排列
column-reverse 竖向、反向排列
row 横向排列
row-reverse 横向、反向排列
(不设置flexDirection时,默认是column)

当flexDirection = column

export default class demo extends Component {
  render() {
    return (
      <View style={{flexDirection:'column'}}>
        <View style={{width:50,height:50,backgroundColor:"red"}}/>
        <View style={{width:50,height:50,backgroundColor:"black"}}/>
        <View style={{width:50,height:50,backgroundColor:"green"}}/>
      </View>
    );
  }
}
flexDirection = column

当flexDirection = column-reverse

export default class demo extends Component {
  render() {
    return (
      <View style={{flexDirection:'column-reverse'}}>
        <View style={{width:50,height:50,backgroundColor:"red"}}/>
        <View style={{width:50,height:50,backgroundColor:"black"}}/>
        <View style={{width:50,height:50,backgroundColor:"green"}}/>
      </View>
    );
  }
}
flexDirection column-reverse

当flexDirection = row

export default class demo extends Component {
  render() {
    return (
      <View style={{flexDirection:'row'}}>
        <View style={{width:50,height:50,backgroundColor:"red"}}/>
        <View style={{width:50,height:50,backgroundColor:"black"}}/>
        <View style={{width:50,height:50,backgroundColor:"green"}}/>
      </View>
    );
  }
}
flexDirection = row

当flexDirection = row-reverse

export default class demo extends Component {
  render() {
    return (
      <View style={{flexDirection:'row-reverse'}}>
        <View style={{width:50,height:50,backgroundColor:"red"}}/>
        <View style={{width:50,height:50,backgroundColor:"black"}}/>
        <View style={{width:50,height:50,backgroundColor:"green"}}/>
      </View>
    );
  }
}
flexDirection row-reverse

3. justifyContent 的取值和使用

决定其子元素沿着主轴的排列方式

假如是垂直排列(由flexDirection决定),那么:

取值:
flex-start 集中在最上方
center 整体竖向居中
flex-end 集中在最下方
space-around 均匀分布
space-between 均匀铺满
(不设置justifyContent时,默认是flex-start)

注意外层容器要加上flex:1,让组件有足够的空间。

当justifyContent = flex-start

export default class demo extends Component {
  render() {
    return (
      <View style={{flex:1,justifyContent:'flex-start',backgroundColor:"pink"}}>
          <View style={{width:50,height:50,backgroundColor:"red"}}/>
          <View style={{width:50,height:50,backgroundColor:"black"}}/>
          <View style={{width:50,height:50,backgroundColor:"green"}}/>
      </View>
    );
  }
}
justifyContent flex-start

当justifyContent = center

export default class demo extends Component {
  render() {
    return (
      <View style={{flex:1,justifyContent:'center',backgroundColor:"pink"}}>
          <View style={{width:50,height:50,backgroundColor:"red"}}/>
          <View style={{width:50,height:50,backgroundColor:"black"}}/>
          <View style={{width:50,height:50,backgroundColor:"green"}}/>
      </View>
    );
  }
}
justifyContent center

当justifyContent = flex-end

export default class demo extends Component {
  render() {
    return (
      <View style={{flex:1,justifyContent:'flex-end',backgroundColor:"pink"}}>
          <View style={{width:50,height:50,backgroundColor:"red"}}/>
          <View style={{width:50,height:50,backgroundColor:"black"}}/>
          <View style={{width:50,height:50,backgroundColor:"green"}}/>
      </View>
    );
  }
}
justifyContent flex-end

当justifyContent = space-around

export default class demo extends Component {
  render() {
    return (
      <View style={{flex:1,justifyContent:'space-around',backgroundColor:"pink"}}>
          <View style={{width:50,height:50,backgroundColor:"red"}}/>
          <View style={{width:50,height:50,backgroundColor:"black"}}/>
          <View style={{width:50,height:50,backgroundColor:"green"}}/>
      </View>
    );
  }
}
justifyContent space-around

当justifyContent = space-between

export default class demo extends Component {
  render() {
    return (
      <View style={{flex:1,justifyContent:'space-between',backgroundColor:"pink"}}>
          <View style={{width:50,height:50,backgroundColor:"red"}}/>
          <View style={{width:50,height:50,backgroundColor:"black"}}/>
          <View style={{width:50,height:50,backgroundColor:"green"}}/>
      </View>
    );
  }
}
justifyContent space-between

4. alignItems的取值和使用

决定其子元素沿着次轴的排列方式,次轴与主轴垂直,意味着当flexDirection为column时,主轴是竖向,次轴是横向,flexDirection为row时,主轴是横向,次轴是竖向。

假如是竖向排列(由flexDirection决定),那么:

取值:

  1. flex-start 集中在最左边
  2. center 整体横向居中
  3. flex-end 集中在最右边
  4. stretch 水平撑满,子组件不能设置固定的width。
    (不设置alignItems时,默认是flex-start)

注意外层容器要加上flex:1,让组件有足够的空间。

当alignItems = flex-start

export default class demo extends Component {
  render() {
    return (
      <View style={{flex:1,alignItems:'flex-start',backgroundColor:"pink"}}>
          <View style={{width:50,height:50,backgroundColor:"red"}}/>
          <View style={{width:50,height:50,backgroundColor:"black"}}/>
          <View style={{width:50,height:50,backgroundColor:"green"}}/>
      </View>
    );
  }
}
alignItems flex-start

当alignItems = center

export default class demo extends Component {
  render() {
    return (
      <View style={{flex:1,alignItems:'center',backgroundColor:"pink"}}>
          <View style={{width:50,height:50,backgroundColor:"red"}}/>
          <View style={{width:50,height:50,backgroundColor:"black"}}/>
          <View style={{width:50,height:50,backgroundColor:"green"}}/>
      </View>
    );
  }
}
alignItems center

当alignItems = flex-end

export default class demo extends Component {
  render() {
    return (
      <View style={{flex:1,alignItems:'flex-end',backgroundColor:"pink"}}>
          <View style={{width:50,height:50,backgroundColor:"red"}}/>
          <View style={{width:50,height:50,backgroundColor:"black"}}/>
          <View style={{width:50,height:50,backgroundColor:"green"}}/>
      </View>
    );
  }
}
alignItems flex-end

当alignItems = stretch

export default class demo extends Component {
  render() {
    return (
      <View style={{flex:1,alignItems:'stretch',backgroundColor:"pink"}}>
          <View style={{height:50,backgroundColor:"red"}}/>
          <View style={{height:50,backgroundColor:"black"}}/>
          <View style={{height:50,backgroundColor:"green"}}/>
      </View>
    );
  }
}
alignItems stretch

5. 他们的结合使用。

看过上面的讲解后,其实知道单个是什么效果,两个属性一起用也就不难理解了。

下面给出一个。
flex 设置为 1 让组件拥有全个屏幕的空间(粉色区域)
flexDirection 设置为 row-reverse 组件横向、反向排列
justifyContent 设置为 space-around 均匀分布
alignItems 设置为 center 水平居中

这样的话,效果就是。

export default class demo extends Component {
  render() {
    return (
      <View style={{flex:1,
                    flexDirection:"row-reverse",
                    justifyContent:'space-around',
                    alignItems:'center',
                    backgroundColor:"pink"}}>
          <View style={{width:50,height:50,backgroundColor:"red"}}/>
          <View style={{width:50,height:50,backgroundColor:"black"}}/>
          <View style={{width:50,height:50,backgroundColor:"green"}}/>
      </View>
    );
  }
}
flex show

以上,完毕。

相关文章

  • React Native学习笔记(day04)

    此篇讲解flex布局系列。 在React Native中,我们使用flexBox布局来适配页面,这个写法和CSS有...

  • 03-Flexible Box

    概念 React-Native 中所有的布局均采用 Flex 布局。采用 Flex 布局的元素,称为 Flex 容...

  • Flex布局

    flex布局前提条件--display: flex(react native 不用设置) 与flex布局相关的属性...

  • React Native - Flex布局讲解

    RN的布局采用的是Flex,弹性盒模型。 有四个主要属性:flex、flexDirection、justifyCo...

  • React Native学习资料

    React 入门实例教程React-Native入门指南Flex 布局教程:语法篇React Native探索(二...

  • Flex布局

    Flex Flex:弹性布局 React Native 的默认布局是flex布局 如果将父元素设置成弹性盒模型模式...

  • React Native - Flex布局

    React Native - Flex布局 Flex布局概述 概念:弹性盒子布局 历史:性盒模型,该布局方案由W3...

  • React Native 之 flexbox布局

    React Native 之 flexbox布局 本文详情本文讲解React Native中的布局,该布局用CSS...

  • 绿侠快充App技术整理

    React Native 原理 ES6 Flex 布局 React Navigation MobX 视频教程 网...

  • React之Flex

    react native 的flex布局,是web的阉割版本,目前还不支持flex-shrink、flex-bas...

网友评论

    本文标题:React Native - Flex布局讲解

    本文链接:https://www.haomeiwen.com/subject/wsnthxtx.html