React Native中佈局採用的是FleBox(彈性框)進行佈局。在React Native中,有4個容器屬性,2個項目屬性,分別是:佈局
容器(父視圖)屬性:flexDirection flexWrap justifyContent alignItemsflex
項目(子視圖)屬性:flex alignSelfspa
一、flexDirection容器屬性: `row | row-reverse | column | column-reverse`,默認爲:'column'code
row:主軸爲水平方向,起點在左端。blog
row-reverse:主軸爲水平方向,起點在右端。繼承
column(默認值):主軸爲垂直方向,起點在上沿。it
column-reverse:主軸爲垂直方向,起點在下沿。io
export default class App extends Component<{}> { render() { return ( <View style={styles.container}> <View style={styles.viewSize}> <Text style={ {fontSize:16}}>1</Text> </View> <View style={styles.viewSize}> <Text style={{fontSize:16}}>2</Text> </View> <View style={styles.viewSize}> <Text style={{fontSize:16}}>3</Text> </View> <View style={styles.viewSize}> <Text style={{fontSize:16}}>4</Text> </View> <View style={styles.viewSize}> <Text style={{fontSize:16}}>5</Text> </View> </View> ); } } const styles = StyleSheet.create({ container: { flexDirection: 'row', backgroundColor: 'gray', marginTop:30 }, viewSize:{ width:40, height:40, backgroundColor:'purple', margin:5 } });
咱們先把flexDirection設置爲row,結果:class
設置爲row-reverse就是反着的容器
設置爲column:
二、flexWrap容器屬性: `nowrap | wrap `
默認狀況下,項目都排在一條線(又稱"軸線")上。flex-wrap屬性定義,若是一條軸線排不下,如何換行。
2.1 nowrap(默認值):不換行:
2.2 wrap:換行,第一行在上方
3:justifyContent容器屬性:`flex-start | flex-end | center | space-between | space-around` ,定義了伸縮項目在主軸線的對齊方式
flex-start(默認值):伸縮項目向一行的起始位置靠齊。
flex-end:伸縮項目向一行的結束位置靠齊。
center:伸縮項目向一行的中間位置靠齊。
space-between:兩端對齊,項目之間的間隔都相等。
space-around:在每行上均勻分配彈性元素。相鄰元素間距離相同。每行第一個元素到行首的距離和每行最後一個元素到行尾的距離將會是相鄰元素之間距離的一半。
四、alignItems容器屬性:`flex-start | flex-end | center | baseline | stretch`定義項目在交叉軸上如何對齊,能夠把其想像成側軸(垂直於主軸)的「對齊方式」。
flex-start:交叉軸的起點對齊。
flex-end:交叉軸的終點對齊 。
center:交叉軸的中點對齊。
baseline:項目的第一行文字的基線對齊。
stretch(默認值):彈性元素被在側軸方向被拉伸到與容器相同的高度或寬度。
五、alignSelf項目屬性:「auto | flex-start | flex-end | center | baseline | stretch」。align-self屬性容許單個項目有與其餘項目不同的對齊方式,可覆蓋align-items屬性。默認值爲auto,表示繼承父元素的align-items屬性,若是沒有父元素,則等同於stretch。
六、flex項目屬性,number,比例。
七、其餘佈局
borderWidth number 邊框寬度
border<Bottom|Left|Right|Top>Color 個方向邊框的顏色
borderColor 邊框顏色