【React Native】FlexBox布局

一、FlexBox布局
  • FlexBox是什么?

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

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

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

【React Native】FlexBox布局_第1张图片
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 (
      
         
            row
            row-reverse  
            column  
            column-reverse   
        
         
           
            
            
            
            
            
             
          
            
            
            
            
            
            
             
          
           
            
            
            
            
            
             
          
           
            
            
            
            
            
             
          
        
       
    );
  }
}

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
    } 
}); 
【React Native】FlexBox布局_第2张图片
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 (
      
         
            flex-start
            flex-end  
            center  
            space-between   
            space-around 
        
         
           
            
            
            
            
            
              
          
            
            
            
            
            
            
              
          
           
            
            
            
            
            
              
          
           
            
            
            
            
            
              
          
          
            
            
            
            
            
              
           
        
       
    );
  }
}

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
    } 
});
【React Native】FlexBox布局_第3张图片
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
  } 
});
【React Native】FlexBox布局_第4张图片
alignItems 效果图.png
  • flexWrap: nowrap | wrap | wrap-reverse
    默认情况下,项目都排在一条线(又称"轴线")上。flex-wrap属性定义,如果一条轴线排不下,如何换行。

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

export default class meituanDemo extends Component {
  render() {
    return (
      
         
            nowrap(默认值)
            wrap   
        
         
           
            
            
            
            
            
              
            
            
            
            
            
              
          
            
            
            
            
            
            
              
            
            
            
            
            
              
           
        
       
    );
  }
}
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
    } 
});
【React Native】FlexBox布局_第5张图片
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 (
       
        
          当前屏幕的宽度:{width + '\n'}
          当前屏幕的高度:{height + '\n'}
          当前屏幕的scale:{scale + '\n'}
        
       
    );
  }
}

const styles = StyleSheet.create({
  contains: { 
    flexDirection: 'row',
    justifyContent: 'center', 
    alignItems: 'center',
    flex: 1,
  },
  welcome: { 
    textAlign: 'center',
    flex: 1
  } 
});
【React Native】FlexBox布局_第6张图片
获取屏幕信息.png
  • 水平居中,垂直居中和水平垂直居中
export default class meituanDemo extends Component {
  render() {
    return (
      
         
            水平居中
            垂直居中   
            水平垂直居中   
        
         
            
             
          
              
             
           
             
              
           
        
       
    );
  }
}
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', 
    }
});

【React Native】FlexBox布局_第7张图片
居中效果图.png

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

你可能感兴趣的:(【React Native】FlexBox布局)