【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進行判斷。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容