React-Native之Flex布局

  • 在React-Native中使用flexbox規(guī)則來指定某個組件的子元素的布局。Flexbox可以在不同屏幕尺寸上提供一致的布局結(jié)構(gòu)。一般來說,使用flexDirectionalignItems、justifyContent三個樣式屬性就已經(jīng)能夠滿足大多數(shù)布局需求。
  • React-Native中的Flexbox的工作原理和web上的CSS基本一致,但是也有一些差異: flexDirection的默認(rèn)值是column,而不是row;flex也只能指定一個數(shù)字值。
1. Flex Direction
  • 在這里需要引入一個主軸的概念,我們可以理解為子組件排列的方向。
    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個間距
    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è)置子組件的width;主軸為水平方向時,不能設(shè)置子組件的height。否則不會生效
    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     // 子組件只允許排列在一行上,可能會溢出
    wrap        // 子組件在一行排列溢出時,就多行排列
    
    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)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,106評論 6 542
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,441評論 3 429
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事?!?“怎么了?”我有些...
    開封第一講書人閱讀 178,211評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,736評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 72,475評論 6 412
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,834評論 1 328
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,829評論 3 446
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 43,009評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,559評論 1 335
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 41,306評論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,516評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,038評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,728評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,132評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,443評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,249評論 3 399
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,484評論 2 379

推薦閱讀更多精彩內(nèi)容

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