React-Native之Flex布局

  • 在React-Native中使用flexbox規(guī)則來(lái)指定某個(gè)組件的子元素的布局。Flexbox可以在不同屏幕尺寸上提供一致的布局結(jié)構(gòu)。一般來(lái)說(shuō),使用flexDirectionalignItems、justifyContent三個(gè)樣式屬性就已經(jīng)能夠滿(mǎn)足大多數(shù)布局需求。
  • React-Native中的Flexbox的工作原理和web上的CSS基本一致,但是也有一些差異: flexDirection的默認(rèn)值是column,而不是row;flex也只能指定一個(gè)數(shù)字值。
1. Flex Direction
  • 在這里需要引入一個(gè)主軸的概念,我們可以理解為子組件排列的方向。
    row                // 主軸為水平方向,從左向右
    row-reverse        // 主軸為水平方向,從右向左
    column             // 主軸為豎直方向,從上到下,默認(rèn)值
    column-reverse     // 主軸為豎直方向,從下到上
    
    export default class Layout_Flex extends Component {
      render() {
        return (
          <View style={{flex: 1, backgroundColor: 'white'}}>
            // flexDirection: 'row',  水平排列
            <View style={{flex:1, flexDirection: 'row', backgroundColor: '#d400ff', paddingTop: 20}}>
              <View style={{width: 100, height: 100, backgroundColor: 'powderblue'}}> </View>
              <View style={{width: 100, height: 100, backgroundColor: 'skyblue'}}> </View>
              <View style={{width: 100, height: 100, backgroundColor: 'steelblue'}}> </View>
            </View>
    
            // flexDirection: 'column',  豎直排列
            <View style={{flex:1, flexDirection: 'column', backgroundColor: '#ffc700', paddingTop: 20}}>
              <View style={{width: 100, height: 100, backgroundColor: 'powderblue'}}> </View>
              <View style={{width: 100, height: 100, backgroundColor: 'skyblue'}}> </View> <View style={{width: 100, height: 100, backgroundColor: 'steelblue'}}> </View>
            </View>
          </View>
        );
      }
    }
    
flexDirection.png
2. Justify Content
  • 決定子組件沿著主軸的排列方式,有以下可選項(xiàng):
    flex-start      //沿主軸方向,從始端向末端排列
    center:        //沿主軸方向,從中心位置向兩頭排列
    flex-end:      //沿主軸方向,從末端向始端排列
    space-around:   //沿主軸方向,等間距排列,首末子組件與父組件相距1/2個(gè)間距
    space-between:  //沿主軸方向,等間距排列,首末子組件與父組件相距0
    
    export default class Layout_Flex extends Component {
     render() {
      return (
        <View style={{flex: 1, backgroundColor: 'white', marginTop: 20}}>
          <View style={{flexDirection: 'column', justifyContent: 'flex-start', backgroundColor: '#ffc700', height: 100}}>
            <View style={{width: 20, height: 20, backgroundColor: 'powderblue'}}></View>
            <View style={{width: 20, height: 20, backgroundColor: 'skyblue'}}></View>
            <View style={{width: 20, height: 20, backgroundColor: 'steelblue'}}></View>
          </View>
          <View style={{flexDirection: 'column', justifyContent: 'center', backgroundColor : '#ff7a00', height: 100}}>
            <View style={{width: 20, height: 20, backgroundColor: 'powderblue'}}></View>
            <View style={{width: 20, height: 20, backgroundColor: 'skyblue'}}></View>
            <View style={{width: 20, height: 20, backgroundColor: 'steelblue'}}></View>
          </View>
          <View style={{flexDirection: 'column', justifyContent: 'flex-end', backgroundColor : '#c4ff00', height: 100}}>
            <View style={{width: 20, height: 20, backgroundColor: 'powderblue'}}></View>
            <View style={{width: 20, height: 20, backgroundColor: 'skyblue'}}></View>
            <View style={{width: 20, height: 20, backgroundColor: 'steelblue'}}></View>
          </View>
          <View style={{flexDirection: 'column', justifyContent: 'space-around', backgroundColor : '#00ffd9', height: 100}}>
            <View style={{width: 20, height: 20, backgroundColor: 'powderblue'}}></View>
            <View style={{width: 20, height: 20, backgroundColor: 'skyblue'}}></View>
            <View style={{width: 20, height: 20, backgroundColor: 'steelblue'}}></View>
          </View>
          <View style={{flexDirection: 'column', justifyContent: 'space-between', backgroundColor : '#008cff', height: 100}}>
            <View style={{width: 20, height: 20, backgroundColor: 'powderblue'}}></View>
            <View style={{width: 20, height: 20, backgroundColor: 'skyblue'}}></View>
            <View style={{width: 20, height: 20, backgroundColor: 'steelblue'}}></View>
          </View>
        </View>
      );
     }
    }
    
  • 主軸方向?yàn)樨Q直方向
justifyContent_column.png
  • 主軸方向?yàn)樗椒较?/li>
justifyContent_row.png
Align Items
  • 決定子組件沿著次軸(與主軸垂直的軸)的排列方式。
    flex-start    // 沿次軸方向,從始端向末端排列
    center        // 沿次軸方向,重中心位置向兩頭排列
    flex-end      // 沿伺候方向,從末端向始端排列
    stretch       // 沿次軸反向,拉伸到與父組件相同高度或?qū)挾?
  • 設(shè)置stretch時(shí),子組件在次軸方向上不能有固定尺寸。即主軸為豎直方向時(shí),不能設(shè)置子組件的width;主軸為水平方向時(shí),不能設(shè)置子組件的height。否則不會(huì)生效
    export default class Layout_Flex extends Component {
     render() {
      return (
        <View style={{flex: 1, backgroundColor: 'white', marginTop: 20}}>
          <View style={{flexDirection: 'column', alignItems: 'flex-start', backgroundColor: '#ffc700', height: 100}}>
            <View style={{width: 20, height: 20, backgroundColor: 'powderblue'}}></View>
            <View style={{width: 20, height: 20, backgroundColor: 'skyblue'}}></View>
            <View style={{width: 20, height: 20, backgroundColor: 'steelblue'}}></View>
          </View>
          <View style={{flexDirection: 'column', alignItems: 'center', backgroundColor : '#ff7a00', height: 100}}>
            <View style={{width: 20, height: 20, backgroundColor: 'powderblue'}}></View>
            <View style={{width: 20, height: 20, backgroundColor: 'skyblue'}}></View>
            <View style={{width: 20, height: 20, backgroundColor: 'steelblue'}}></View>
          </View>
          <View style={{flexDirection: 'column', alignItems: 'flex-end', backgroundColor : '#c4ff00', height: 100}}>
            <View style={{width: 20, height: 20, backgroundColor: 'powderblue'}}></View>
            <View style={{width: 20, height: 20, backgroundColor: 'skyblue'}}></View>
            <View style={{width: 20, height: 20, backgroundColor: 'steelblue'}}></View>
          </View>
          <View style={{flexDirection: 'column', alignItems: 'stretch', backgroundColor : '#00ffd9', height: 100}}>
            <View style={{height: 20, backgroundColor: 'powderblue'}}></View>
            <View style={{height: 20, backgroundColor: 'skyblue'}}></View>
            <View style={{height: 20, backgroundColor: 'steelblue'}}></View>
          </View>
        </View>
      );
     }
    }
    
  • 主軸為豎直方向
alignItems_column.png
  • 主軸為水平方向
alignItems_row.png
4.Flex Wrap
  • 決定子組件在父組件內(nèi)是否允許多行排列
    nowrap     // 子組件只允許排列在一行上,可能會(huì)溢出
    wrap        // 子組件在一行排列溢出時(shí),就多行排列
    
    export default class Layout_Flex extends Component {
     render() {
      return (
        <View style={{flex: 1, backgroundColor: 'white', marginTop: 20}}>
          <View style={{flexDirection: 'row', flexWrap: 'nowrap', backgroundColor: '#ffc700', height: 200}}>
            <View style={{width: 100, height: 50, backgroundColor: 'powderblue', margin: 30}}></View>
            <View style={{width: 100, height: 50, backgroundColor: 'skyblue', margin: 30}}></View>
            <View style={{width: 100, height: 50, backgroundColor: 'steelblue', margin: 30}}></View>
          </View>
          <View style={{flexDirection: 'row', flexWrap: 'wrap', backgroundColor : '#ff7a00', height: 200}}>
            <View style={{width: 100, height: 50, backgroundColor: 'powderblue', margin: 30}}></View>
            <View style={{width: 100, height: 50, backgroundColor: 'skyblue', margin: 30}}></View>
            <View style={{width: 100, height: 50, backgroundColor: 'steelblue', margin: 30}}></View>
          </View>
        </View>
      );
     }
    }
    
flexWrap.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 前言 學(xué)習(xí)本系列內(nèi)容需要具備一定 HTML 開(kāi)發(fā)基礎(chǔ),沒(méi)有基礎(chǔ)的朋友可以先轉(zhuǎn)至 HTML快速入門(mén)(一) 學(xué)習(xí) 本人...
    珍此良辰閱讀 4,661評(píng)論 2 19
  • Flex是Flexible Box的縮寫(xiě),意為"彈性布局",用來(lái)為盒狀模型提供最大的靈活性。Flex布局主要思想是...
    賈里閱讀 1,138評(píng)論 0 0
  • 一、FlexBox布局 FlexBox是什么? 彈性盒模型(The Flexible Box Module),又叫...
    lever_xu閱讀 1,027評(píng)論 0 0
  • 本文只是簡(jiǎn)單地介紹下在React-Native中,使用CSS的Flex布局方式,完成RN中的控件布局,掌握這個(gè)布局...
    劉是丑閱讀 1,195評(píng)論 0 1
  • 一般使用ReactNative開(kāi)發(fā)App,一般都采用Flex布局,使用這套布局就非???。Flex簡(jiǎn)介Flex又叫彈...
    因幡白兔閱讀 973評(píng)論 6 8

友情鏈接更多精彩內(nèi)容