使用Flexbox布局

React Native中使用flexbox規(guī)則來制定某個(gè)組件的子元素的布局残邀。

使用flexDirection嗅定、alignItems和justifyContent三個(gè)樣式屬性就能滿足大多數(shù)布局的需要慷丽。

下面看看到底怎么用這些屬性布局:

首先定義四個(gè)view赎瑰,一個(gè)大小是主屏幕大小,顏色為紅色,其他都是長(zhǎng)寬為50的view召庞,顏色分別是藍(lán)色锡垄、綠色稚虎、黃色。顏色雖然有點(diǎn)土偎捎,顏色明顯蠢终,希望能看懂P蛉痢!寻拂!

1.????flexDirection可以決定布局的主軸程奠。子元素是應(yīng)該沿著水平軸(row)方向排列還是豎軸(column)方向排列。

<View?style={{flex:1,flexDirection:'row',backgroundColor:'red'}}>

?<View?style={{width:?50,?height:?50,?backgroundColor:?'blue'}}/>

?<View?style={{width:?50,?height:?50,?backgroundColor:?'green'}}/>

?<View?style={{width:?50,?height:?50,?backgroundColor:?'yellow'}}/>

</View>

flexDirection:’row’祭钉、flexDirection:’row-reverse’顯示的如下圖


row


row-reverse

<View style={{flex: 1, flexDirection: 'column', backgroundColor:'red'}}>

?? <View style={{width: 50, height: 50, backgroundColor: 'blue'}} />

?? <View style={{width: 50, height: 50, backgroundColor: 'green'}} />

??? <View style={{width: 50, height: 50, backgroundColor: 'yellow'}} />

</View>

flexDirection:’column’瞄沙、flexDirection:’column-reverse’顯示如下圖:


column


column-reverse

2.????Justify Content

在組件的style中制定justifyContent可以決定其子元素沿著主軸的排列方式。子元素是應(yīng)該靠近主軸的起始端還是末尾段分布慌核,對(duì)應(yīng)項(xiàng)有flex-start距境、center、flex-end垮卓、space-around以及space-between垫桂。沒有設(shè)置flexDirection默認(rèn)是主軸是縱向的。

1)flex-start和默認(rèn)的一樣在開始處排列粟按。

<View style={{flex: 1,justifyContent:'flex-start',backgroundColor:'red'}}>

?<View style={{width: 50, height: 50, backgroundColor: 'blue'}}/>

<View style={{width: 50, height: 50, backgroundColor: 'green'}}/>

<View style={{width: 50, height: 50, backgroundColor: 'yellow'}}/>

</View>



justifyContent:'flex-start'

2)flex-end

<View style={{flex: 1,justifyContent:'flex-end',backgroundColor:'red'}}>

<View style={{width: 50, height: 50, backgroundColor: 'blue'}}/>

?<View style={{width: 50, height: 50, backgroundColor: 'green'}}/>

? <View style={{width: 50, height: 50, backgroundColor: 'yellow'}}/>

</View>


justifyContent:'flex-end'

3)center

<View style={{flex: 1,justifyContent:'center',backgroundColor:'red'}}>

?? <View style={{width: 50, height: 50, backgroundColor: 'blue'}} />

?? <View style={{width: 50, height: 50, backgroundColor: 'green'}} />

??? <View style={{width: 50, height: 50, backgroundColor: 'yellow'}}/>

</View>

justifyContent:'center'

4)space-around

<View style={{flex: 1,justifyContent:'space-around',backgroundColor:'red'}}>

<View style={{width: 50, height: 50, backgroundColor: 'blue'}}/><View style={{width: 50, height: 50, backgroundColor: 'green'}}/><View style={{width: 50, height: 50, backgroundColor: 'yellow'}}/>

</View>

,justifyContent:'space-around'

5)space-between

<View style={{flex: 1,justifyContent:'space-between',backgroundColor:'red'}}>

<View style={{width: 50, height: 50, backgroundColor: 'blue'}} />

<View style={{width: 50, height: 50, backgroundColor: 'green'}} />

?<View style={{width: 50, height: 50, backgroundColor: 'yellow'}} />

</View>

justifyContent:'space-between'

<View style={{flex: 1,alignItems:'center',backgroundColor:'red'}}>

?<View style={{width: 50, height: 50, backgroundColor: 'blue'}} />

?<View style={{width: 50, height: 50, backgroundColor: 'green'}} />

? <View style={{width: 50, height: 50, backgroundColor: 'yellow'}} />

</View>

alignItems:'center'

<View style={{flex: 1,alignItems:'flex-start',backgroundColor:'red'}}>

?<View style={{width: 50, height: 50, backgroundColor: 'blue'}} />

?<View style={{width: 50, height: 50, backgroundColor: 'green'}} />

? <View style={{width: 50, height: 50, backgroundColor: 'yellow'}}/>

</View>

alignItems:'flex-start'

<View style={{flex: 1,alignItems:'flex-end',backgroundColor:'red'}}>

?<View style={{width: 50, height: 50, backgroundColor: 'blue'}} /> <View style={{width: 50, height: 50, backgroundColor: 'green'}} />

<View style={{width: 50, height: 50, backgroundColor: 'yellow'}}/>

</View>


alignItems:'flex-end'

<View?style={{flex: 1,alignItems:'flex-start',backgroundColor:'red'}}>

<View?style={{width: 50,?height: 50,?backgroundColor:?'blue'}} /> <View?style={{width: 50,?height: 50,?backgroundColor:?'green'}} />

<View?style={{width: 50,?height: 50,?backgroundColor:?'yellow'}}/>

</View>


alignItems:'flex-start'

<View?style={{flex: 1,alignItems:'flex-start',backgroundColor:'red'}}>

<View?style={{width: 50,height: 50,backgroundColor:'blue'}} /> <View?style={{width: 50,height: 50,backgroundColor:'green'}} />

<View?style={{width: 50,height: 50,backgroundColor:'yellow'}}/>


alignItems:'flex-start'

以上就是根據(jù)參數(shù)編寫程序的效果圖诬滩,供理解和參考。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末灭将,一起剝皮案震驚了整個(gè)濱河市疼鸟,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌庙曙,老刑警劉巖空镜,帶你破解...
    沈念sama閱讀 218,525評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異捌朴,居然都是意外死亡姑裂,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,203評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門男旗,熙熙樓的掌柜王于貴愁眉苦臉地迎上來舶斧,“玉大人,你說我怎么就攤上這事察皇≤罾鳎” “怎么了?”我有些...
    開封第一講書人閱讀 164,862評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵什荣,是天一觀的道長(zhǎng)矾缓。 經(jīng)常有香客問我,道長(zhǎng)稻爬,這世上最難降的妖魔是什么嗜闻? 我笑而不...
    開封第一講書人閱讀 58,728評(píng)論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮桅锄,結(jié)果婚禮上琉雳,老公的妹妹穿的比我還像新娘样眠。我一直安慰自己,他們只是感情好翠肘,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,743評(píng)論 6 392
  • 文/花漫 我一把揭開白布檐束。 她就那樣靜靜地躺著,像睡著了一般束倍。 火紅的嫁衣襯著肌膚如雪被丧。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,590評(píng)論 1 305
  • 那天绪妹,我揣著相機(jī)與錄音甥桂,去河邊找鬼。 笑死邮旷,一個(gè)胖子當(dāng)著我的面吹牛黄选,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播廊移,決...
    沈念sama閱讀 40,330評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼糕簿,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼探入!你這毒婦竟也來了狡孔?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,244評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤蜂嗽,失蹤者是張志新(化名)和其女友劉穎苗膝,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體植旧,經(jīng)...
    沈念sama閱讀 45,693評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡辱揭,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,885評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了病附。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片问窃。...
    茶點(diǎn)故事閱讀 40,001評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖完沪,靈堂內(nèi)的尸體忽然破棺而出域庇,到底是詐尸還是另有隱情,我是刑警寧澤覆积,帶...
    沈念sama閱讀 35,723評(píng)論 5 346
  • 正文 年R本政府宣布听皿,位于F島的核電站,受9級(jí)特大地震影響宽档,放射性物質(zhì)發(fā)生泄漏尉姨。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,343評(píng)論 3 330
  • 文/蒙蒙 一吗冤、第九天 我趴在偏房一處隱蔽的房頂上張望又厉。 院中可真熱鬧九府,春花似錦、人聲如沸馋没。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,919評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽篷朵。三九已至勾怒,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間声旺,已是汗流浹背笔链。 一陣腳步聲響...
    開封第一講書人閱讀 33,042評(píng)論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留腮猖,地道東北人鉴扫。 一個(gè)月前我還...
    沈念sama閱讀 48,191評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像澈缺,于是被迫代替她去往敵國和親坪创。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,955評(píng)論 2 355

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

  • 前言 學(xué)習(xí)本系列內(nèi)容需要具備一定 HTML 開發(fā)基礎(chǔ)姐赡,沒有基礎(chǔ)的朋友可以先轉(zhuǎn)至 HTML快速入門(一) 學(xué)習(xí) 本人...
    珍此良辰閱讀 4,524評(píng)論 2 19
  • 1莱预、ReactNative中能使用的css樣式 "alignItems","alignSelf","backfac...
    babybus_superdo閱讀 998評(píng)論 0 1
  • 在React-Native中使用flexbox規(guī)則來指定某個(gè)組件的子元素的布局。Flexbox可以在不同屏幕尺寸上...
    Coder_Answer閱讀 1,512評(píng)論 1 2
  • 我們?cè)?React Native 中使用 flexbox 規(guī)則來指定某個(gè)組件的子元素的布局项滑。Flexbox 可以在...
    藍(lán)白七七閱讀 542評(píng)論 0 0
  • 我們?cè)赗eact Native中使用flexbox規(guī)則來指定某個(gè)組件的子元素的布局依沮。Flexbox可以在不同屏幕尺...
    冷洪林閱讀 296評(píng)論 0 1