【React Native】FlexBox布局

一、FlexBox布局
  • FlexBox是什么?

弹性盒模型(The Flexible Box Module),又叫Flexbox,意为“弹性布局”,旨在通过弹性的方式来对齐和分布容器中内容的空间,使其能适应不同屏幕,为盒装模型提供最大的灵活性。
Flex布局主要思想是:让容器有能力让其子项目能够改变其宽度、高度(甚至是顺序),以最佳方式填充可用空间;
React native中的FlexBox是这个规范的一个子集。

二、Flexbox在开发中的应用场景

  • Flexbox在布局中能够解决什么问题?
    浮动布局
    各种机型屏幕的适配
    水平和垂直居中
    自动分配宽度
    ......
  • 在CSS中,常规的布局是基于块和内联流方向,而Flex布局是基于flex-flow流,下图很好解释了Flex布局的思想

1.png

容器默认存在两根轴:水平的主轴(main axis)和垂直的交叉轴(cross axis)。主轴的开始位置(与边框的交叉点)叫做main start,结束位置叫做main end;交叉轴的开始位置叫做cross start,结束位置叫做cross end
项目默认沿主轴排列。单个项目占据的主轴空间叫做main size,占据的交叉轴空间叫做cross size

三、Flexbox的常用属性

  • 容器属性

  • flexDirection: row | row-reverse | column | column-reverse
    该属性决定主轴的方向(即项目的排列方向)。
    主要是父视图来控制子视图的显示

    row:主轴为水平方向,起点在左端;
    row-reverse:主轴为水平方向,起点在右端;
    column(默认值):主轴为垂直方向,起点在上沿。
    column-reverse:主轴为垂直方向,起点在下沿。

export default class meituanDemo extends Component {
  render() {
    return (
      <View style={styles.contains}>
        <View style={styles.title}> 
            <Text style={{width: 80, textAlign: 'center'}}>row</Text>
            <Text style={{width: 80, textAlign: 'center'}}>row-reverse</Text>  
            <Text style={{width: 80, textAlign: 'center'}}>column</Text>  
            <Text style={{width: 80, textAlign: 'center'}}>column-reverse</Text>   
        </View>
        <View style={styles.example}> 
          <View style={styles.one}> 
            <View style={styles.top}>
            </View>
            <View style={styles.middle}>
            </View>
            <View style={styles.bottom}>
            </View> 
          </View>
          <View style={styles.two}>  
            <View style={styles.top}>
            </View>
            <View style={styles.middle}>
            </View>
            <View style={styles.bottom}>
            </View> 
          </View>
          <View style={styles.three}> 
            <View style={styles.top}>
            </View>
            <View style={styles.middle}>
            </View>
            <View style={styles.bottom}>
            </View> 
          </View>
          <View style={styles.four}> 
            <View style={styles.top}>
            </View>
            <View style={styles.middle}>
            </View>
            <View style={styles.bottom}>
            </View> 
          </View>
        </View>
      </View> 
    );
  }
}

const styles = StyleSheet.create({
    contains: { 
      flexDirection: 'column',
      // justifyContent: 'space-around', 
      // alignItems: 'center',
      flex: 1
    },
    title: { 
      flexDirection: 'row',
      justifyContent: 'space-around', 
      alignItems: 'center', 
      height: 30,
      marginTop: 200
    },
    example: {
      flexDirection: 'row',
      justifyContent: 'space-around', 
      alignItems: 'center', 
      height: 200,
    },
    one: {
      backgroundColor: 'yellow',
      width: 80,
      height: 200,
      flexDirection: 'row'
    },
    two: {
      backgroundColor: 'purple',
      width: 80,
      height: 200,
      flexDirection: 'row-reverse'
    },
    three: {
      backgroundColor: 'blue',
      width: 80,
      height: 200,
      flexDirection: 'column'
    },
    four: {
      backgroundColor: 'red',
      width: 80,
      height: 200,
      flexDirection: 'column-reverse'
    },
    top: {
      backgroundColor: 'green',
      flex: 1
    },
    middle: {
      backgroundColor: 'black',
      flex: 1
    },
    bottom: {
      backgroundColor: 'orange',
      flex: 1
    } 
}); 
flexDirection 效果图.png
  • justifyContent:flex-start | flex-end | center | space-between | space-around
    定义了伸缩项目在主轴线的对齐方式:

flex-start(默认值):伸缩项目向一行的起始位置靠齐。
flex-end:伸缩项目向一行的结束位置靠齐。
center:伸缩项目向一行的中间位置靠齐。
space-between:两端对齐,项目之间的间隔都相等。
space-around:伸缩项目会平均地分布在行里,两端保留一半的空间。

export default class meituanDemo extends Component {
  render() {
    return (
      <View style={styles.contains}>
        <View style={styles.title}> 
            <Text style={{height: 30, textAlign: 'center'}}>flex-start</Text>
            <Text style={{height: 30, textAlign: 'center'}}>flex-end</Text>  
            <Text style={{height: 30, textAlign: 'center'}}>center</Text>  
            <Text style={{height: 30, textAlign: 'center'}}>space-between</Text>   
            <Text style={{height: 30, textAlign: 'center'}}>space-around</Text> 
        </View>
        <View style={styles.example}> 
          <View style={styles.one}> 
            <View style={styles.left}>
            </View>
            <View style={styles.middle}>
            </View>
            <View style={styles.right}>
            </View>  
          </View>
          <View style={styles.two}>  
            <View style={styles.left}>
            </View>
            <View style={styles.middle}>
            </View>
            <View style={styles.right}>
            </View>  
          </View>
          <View style={styles.three}> 
            <View style={styles.left}>
            </View>
            <View style={styles.middle}>
            </View>
            <View style={styles.right}>
            </View>  
          </View>
          <View style={styles.four}> 
            <View style={styles.left}>
            </View>
            <View style={styles.middle}>
            </View>
            <View style={styles.right}>
            </View>  
          </View>
          <View style={styles.five}>
            <View style={styles.left}>
            </View>
            <View style={styles.middle}>
            </View>
            <View style={styles.right}>
            </View>  
          </View> 
        </View>
      </View> 
    );
  }
}

const styles = StyleSheet.create({
    contains: { 
      flexDirection: 'row',
      justifyContent: 'space-around', 
      alignItems: 'center',
      flex: 1
    },
    title: { 
      flexDirection: 'column',
      justifyContent: 'space-around', 
      alignItems: 'center', 
      width: 100,
      height: 300 
    },
    example: {
      flexDirection: 'column',
      justifyContent: 'space-around',  
      flex: 1, 
      height: 300,
    },
    one: {
      backgroundColor: 'gray', 
      height: 50,  
      justifyContent: 'flex-start',
      flexDirection: 'row',
    },
    two: {
      backgroundColor: 'gray', 
      height: 50, 
      justifyContent: 'flex-end',
      flexDirection: 'row',
    },
    three: {
      backgroundColor: 'gray', 
      height: 50, 
      justifyContent: 'center',
      flexDirection: 'row',
    },
    four: {
      backgroundColor: 'gray', 
      height: 50, 
      justifyContent: 'space-between',
      flexDirection: 'row',
    },
    five: {
      backgroundColor: 'gray', 
      height: 50, 
      justifyContent: 'space-around',
      flexDirection: 'row',
    },
    left: {
      backgroundColor: 'green', 
      width: 50
    },
    middle: {
      backgroundColor: 'black', 
      width: 50
    },
    right: {
      backgroundColor: 'orange', 
      width: 50
    } 
});
justifyContent 效果图.png
  • alignItems: flex-start | flex-end | center | baseline | stretch
    定义项目在交叉轴上如何对齐,可以把其想像成侧轴(垂直于主轴)的“对齐方式”。

flex-start:交叉轴的起点对齐。
flex-end:交叉轴的终点对齐 。
center:交叉轴的中点对齐。
baseline:项目的第一行文字的基线对齐。
stretch(默认值):如果项目未设置高度或设为auto,将占满整个容器的高度。

const styles = StyleSheet.create({
  contains: { 
    flexDirection: 'row',
    justifyContent: 'space-around', 
    alignItems: 'center',
    flex: 1
  },
  title: { 
    flexDirection: 'column',
    justifyContent: 'space-around', 
    alignItems: 'center', 
    width: 100,
    height: 300 
  },
  example: {
    flexDirection: 'column',
    justifyContent: 'space-around',  
    flex: 1, 
    height: 300,
  },
  one: {
    backgroundColor: 'gray', 
    height: 50,  
    justifyContent: 'flex-start',
    flexDirection: 'row',
    alignItems: 'flex-start'
  },
  two: {
    backgroundColor: 'gray', 
    height: 50, 
    justifyContent: 'flex-end',
    flexDirection: 'row',
    alignItems: 'flex-end'
  },
  three: {
    backgroundColor: 'gray', 
    height: 50, 
    justifyContent: 'center',
    flexDirection: 'row',
    alignItems: 'center'
  },
  four: {
    backgroundColor: 'gray', 
    height: 50, 
    justifyContent: 'space-between',
    flexDirection: 'row',
    alignItems: 'baseline'
  },
  five: {
    backgroundColor: 'gray', 
    height: 50, 
    justifyContent: 'space-around',
    flexDirection: 'row',
    alignItems: 'stretch'
  },
  left: {
    backgroundColor: 'green', 
    width: 50,
    height: 30
  },
  middle: {
    backgroundColor: 'black', 
    width: 50,
    height: 20
  },
  right: {
    backgroundColor: 'orange', 
    width: 50,
    height: 50
  } 
});
alignItems 效果图.png
  • flexWrap: nowrap | wrap | wrap-reverse
    默认情况下,项目都排在一条线(又称"轴线")上。flex-wrap属性定义,如果一条轴线排不下,如何换行。

nowrap(默认值):不换行。
wrap:换行,第一行在上方。

export default class meituanDemo extends Component {
  render() {
    return (
      <View style={styles.contains}>
        <View style={styles.title}> 
            <Text style={{height: 30, textAlign: 'center'}}>nowrap(默认值)</Text>
            <Text style={{height: 30, textAlign: 'center'}}>wrap</Text>   
        </View>
        <View style={styles.example}> 
          <View style={styles.one}> 
            <View style={{backgroundColor: 'green',width: 50,height: 20,marginLeft:10,marginTop:2}}>
            </View>
            <View style={{backgroundColor: 'green',width: 50,height: 20,marginLeft:10,marginTop:2}}>
            </View>
            <View style={{backgroundColor: 'green',width: 50,height: 20,marginLeft:10,marginTop:2}}>
            </View>  
            <View style={{backgroundColor: 'green',width: 50,height: 20,marginLeft:10,marginTop:2}}>
            </View>
            <View style={{backgroundColor: 'green',width: 50,height: 20,marginLeft:10,marginTop:2}}>
            </View>
            <View style={{backgroundColor: 'green',width: 50,height: 20,marginLeft:10,marginTop:2}}>
            </View>  
          </View>
          <View style={styles.two}>  
            <View style={{backgroundColor: 'green',width: 50,height: 20,marginLeft:10,marginTop:2}}>
            </View>
            <View style={{backgroundColor: 'green',width: 50,height: 20,marginLeft:10,marginTop:2}}>
            </View>
            <View style={{backgroundColor: 'green',width: 50,height: 20,marginLeft:10,marginTop:2}}>
            </View>  
            <View style={{backgroundColor: 'green',width: 50,height: 20,marginLeft:10,marginTop:2}}>
            </View>
            <View style={{backgroundColor: 'green',width: 50,height: 20,marginLeft:10,marginTop:2}}>
            </View>
            <View style={{backgroundColor: 'green',width: 50,height: 20,marginLeft:10,marginTop:2}}>
            </View>  
          </View> 
        </View>
      </View> 
    );
  }
}
const styles = StyleSheet.create({
    contains: { 
      flexDirection: 'row',
      justifyContent: 'space-around', 
      alignItems: 'center',
      flex: 1
    },
    title: { 
      flexDirection: 'column',
      justifyContent: 'space-around', 
      alignItems: 'center', 
      width: 100,
      height: 300 
    },
    example: {
      flexDirection: 'column',
      justifyContent: 'space-around',  
      flex: 1, 
      height: 300,
    },
    one: {
      backgroundColor: 'gray', 
      height: 50,  
      justifyContent: 'flex-start',
      flexDirection: 'row',
      flexWrap: 'nowrap'
    },
    two: {
      backgroundColor: 'gray', 
      height: 50, 
      justifyContent: 'flex-end',
      flexDirection: 'row',
      flexWrap: 'wrap'
    },
    three: {
      backgroundColor: 'gray', 
      height: 50, 
      justifyContent: 'center',
      flexDirection: 'row',  
    },
    four: {
      backgroundColor: 'gray', 
      height: 50, 
      justifyContent: 'space-between',
      flexDirection: 'row',
      alignItems: 'baseline'
    },
    five: {
      backgroundColor: 'gray', 
      height: 50, 
      justifyContent: 'space-around',
      flexDirection: 'row',
      alignItems: 'stretch'
    },
    left: {
      backgroundColor: 'green', 
      width: 50,
      height: 30
    },
    middle: {
      backgroundColor: 'black', 
      width: 50,
      height: 20
    },
    right: {
      backgroundColor: 'orange', 
      width: 50,
      height: 50
    } 
});
flexWrap 效果图.png
  • 元素属性

    • flex
      flex属性是flex-grow, flex-shrinkflex-basis的简写,默认值为0 1 auto。后两个属性可选。 默认值为“0 1 auto”。

    宽度 = 弹性宽度 * ( flexGrow / sum( flexGorw ) )

    • alignSelf: “auto | flex-start | flex-end | center | baseline | stretch
      align-self属性允许单个项目有与其他项目不一样的对齐方式,可覆盖align-items属性。默认值为auto,表示继承父元素的align-items属性,如果没有父元素,则等同于stretch

四、在React Native 中使用FlexlBox布局

  • 获取当前屏幕的宽度、高度和scale
var {width, height, scale} = Dimensions.get('window')
export default class meituanDemo extends Component {
  render() {
    return (
      <View style={styles.contains}> 
        <Text style={styles.welcome}>
          当前屏幕的宽度:{width + '\n'}
          当前屏幕的高度:{height + '\n'}
          当前屏幕的scale:{scale + '\n'}
        </Text>
      </View> 
    );
  }
}

const styles = StyleSheet.create({
  contains: { 
    flexDirection: 'row',
    justifyContent: 'center', 
    alignItems: 'center',
    flex: 1,
  },
  welcome: { 
    textAlign: 'center',
    flex: 1
  } 
});
获取屏幕信息.png
  • 水平居中,垂直居中和水平垂直居中
export default class meituanDemo extends Component {
  render() {
    return (
      <View style={styles.contains}>
        <View style={styles.title}> 
            <Text style={{height: 30, textAlign: 'center'}}>水平居中</Text>
            <Text style={{height: 30, textAlign: 'center'}}>垂直居中</Text>   
            <Text style={{height: 30, textAlign: 'center'}}>水平垂直居中</Text>   
        </View>
        <View style={styles.example}> 
          <View style={styles.one}>  
            <Image source={{uri:'http://farm4.staticflickr.com/3795/9269794168_3ac58fc15c_b.jpg'}} style={{width:30,height:30}} /> 
          </View>
          <View style={styles.two}>    
            <Image source={{uri:'http://farm4.staticflickr.com/3795/9269794168_3ac58fc15c_b.jpg'}} style={{width:30,height:30}} /> 
          </View> 
          <View style={styles.three}>   
            <Image source={{uri:'http://farm4.staticflickr.com/3795/9269794168_3ac58fc15c_b.jpg'}} style={{width:30,height:30}} />  
          </View> 
        </View>
      </View> 
    );
  }
}
const styles = StyleSheet.create({
    contains: { 
      flexDirection: 'row',
      justifyContent: 'space-around', 
      alignItems: 'center',
      flex: 1
    },
    title: { 
      flexDirection: 'column',
      justifyContent: 'space-around', 
      alignItems: 'center', 
      width: 100,
      height: 300 
    },
    example: {
      flexDirection: 'column',
      justifyContent: 'space-around',  
      flex: 1, 
      height: 300,
    },
    one: {
      backgroundColor: 'gray', 
      height: 50,   
      alignItems: 'center'
    },
    two: {
      backgroundColor: 'gray', 
      height: 50, 
      justifyContent: 'center', 
    },
    three: {
      backgroundColor: 'gray', 
      height: 50, 
      alignItems: 'center',
      justifyContent: 'center', 
    }
});

居中效果图.png

备注:一旦设置alignItems属性之后,组件的大小包裹随着内容的尺寸;此外水平居中和垂直居中还要结合FlexDirection进行判断。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容