- 在React-Native中使用flexbox規(guī)則來指定某個組件的子元素的布局。Flexbox可以在不同屏幕尺寸上提供一致的布局結(jié)構(gòu)。一般來說,使用
flexDirection
、alignItems
、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