一、FlexBox布局
-
FlexBox是什么?
彈性盒模型(The Flexible Box Module),又叫Flexbox,意為“彈性布局”,旨在通過彈性的方式來對(duì)齊和分布容器中內(nèi)容的空間,使其能適應(yīng)不同屏幕,為盒裝模型提供最大的靈活性。
Flex布局主要思想是:讓容器有能力讓其子項(xiàng)目能夠改變其寬度、高度(甚至是順序),以最佳方式填充可用空間;
React native中的FlexBox是這個(gè)規(guī)范的一個(gè)子集。
二、Flexbox在開發(fā)中的應(yīng)用場景
-
Flexbox在布局中能夠解決什么問題?
浮動(dòng)布局
各種機(jī)型屏幕的適配
水平和垂直居中
自動(dòng)分配寬度
...... -
在CSS中,常規(guī)的布局是基于塊和內(nèi)聯(lián)流方向,而Flex布局是基于flex-flow流,下圖很好解釋了Flex布局的思想
容器默認(rèn)存在兩根軸:水平的主軸(
main axis
)和垂直的交叉軸(cross axis
)。主軸的開始位置(與邊框的交叉點(diǎn))叫做main star
t,結(jié)束位置叫做main end
;交叉軸的開始位置叫做cross start
,結(jié)束位置叫做cross end
。項(xiàng)目默認(rèn)沿主軸排列。單個(gè)項(xiàng)目占據(jù)的主軸空間叫做
main size
,占據(jù)的交叉軸空間叫做cross size
。
三、Flexbox的常用屬性
容器屬性
-
flexDirection:
row | row-reverse | column | column-reverse
該屬性決定主軸的方向(即項(xiàng)目的排列方向)。
主要是父視圖來控制子視圖的顯示row
:主軸為水平方向,起點(diǎn)在左端;
row-reverse
:主軸為水平方向,起點(diǎn)在右端;
column
(默認(rèn)值):主軸為垂直方向,起點(diǎn)在上沿。
column-reverse
:主軸為垂直方向,起點(diǎn)在下沿。
export default class meituanDemo extends Component {
render() {
return (
<View style={styles.contains}>
<View style={styles.title}>
<Text style={{width: 80, textAlign: 'center'}}>row</Text>
<Text style={{width: 80, textAlign: 'center'}}>row-reverse</Text>
<Text style={{width: 80, textAlign: 'center'}}>column</Text>
<Text style={{width: 80, textAlign: 'center'}}>column-reverse</Text>
</View>
<View style={styles.example}>
<View style={styles.one}>
<View style={styles.top}>
</View>
<View style={styles.middle}>
</View>
<View style={styles.bottom}>
</View>
</View>
<View style={styles.two}>
<View style={styles.top}>
</View>
<View style={styles.middle}>
</View>
<View style={styles.bottom}>
</View>
</View>
<View style={styles.three}>
<View style={styles.top}>
</View>
<View style={styles.middle}>
</View>
<View style={styles.bottom}>
</View>
</View>
<View style={styles.four}>
<View style={styles.top}>
</View>
<View style={styles.middle}>
</View>
<View style={styles.bottom}>
</View>
</View>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
contains: {
flexDirection: 'column',
// justifyContent: 'space-around',
// alignItems: 'center',
flex: 1
},
title: {
flexDirection: 'row',
justifyContent: 'space-around',
alignItems: 'center',
height: 30,
marginTop: 200
},
example: {
flexDirection: 'row',
justifyContent: 'space-around',
alignItems: 'center',
height: 200,
},
one: {
backgroundColor: 'yellow',
width: 80,
height: 200,
flexDirection: 'row'
},
two: {
backgroundColor: 'purple',
width: 80,
height: 200,
flexDirection: 'row-reverse'
},
three: {
backgroundColor: 'blue',
width: 80,
height: 200,
flexDirection: 'column'
},
four: {
backgroundColor: 'red',
width: 80,
height: 200,
flexDirection: 'column-reverse'
},
top: {
backgroundColor: 'green',
flex: 1
},
middle: {
backgroundColor: 'black',
flex: 1
},
bottom: {
backgroundColor: 'orange',
flex: 1
}
});
- justifyContent:
flex-start | flex-end | center | space-between | space-around
定義了伸縮項(xiàng)目在主軸線的對(duì)齊方式:
flex-start(默認(rèn)值):伸縮項(xiàng)目向一行的起始位置靠齊。
flex-end:伸縮項(xiàng)目向一行的結(jié)束位置靠齊。
center:伸縮項(xiàng)目向一行的中間位置靠齊。
space-between:兩端對(duì)齊,項(xiàng)目之間的間隔都相等。
space-around:伸縮項(xiàng)目會(huì)平均地分布在行里,兩端保留一半的空間。
export default class meituanDemo extends Component {
render() {
return (
<View style={styles.contains}>
<View style={styles.title}>
<Text style={{height: 30, textAlign: 'center'}}>flex-start</Text>
<Text style={{height: 30, textAlign: 'center'}}>flex-end</Text>
<Text style={{height: 30, textAlign: 'center'}}>center</Text>
<Text style={{height: 30, textAlign: 'center'}}>space-between</Text>
<Text style={{height: 30, textAlign: 'center'}}>space-around</Text>
</View>
<View style={styles.example}>
<View style={styles.one}>
<View style={styles.left}>
</View>
<View style={styles.middle}>
</View>
<View style={styles.right}>
</View>
</View>
<View style={styles.two}>
<View style={styles.left}>
</View>
<View style={styles.middle}>
</View>
<View style={styles.right}>
</View>
</View>
<View style={styles.three}>
<View style={styles.left}>
</View>
<View style={styles.middle}>
</View>
<View style={styles.right}>
</View>
</View>
<View style={styles.four}>
<View style={styles.left}>
</View>
<View style={styles.middle}>
</View>
<View style={styles.right}>
</View>
</View>
<View style={styles.five}>
<View style={styles.left}>
</View>
<View style={styles.middle}>
</View>
<View style={styles.right}>
</View>
</View>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
contains: {
flexDirection: 'row',
justifyContent: 'space-around',
alignItems: 'center',
flex: 1
},
title: {
flexDirection: 'column',
justifyContent: 'space-around',
alignItems: 'center',
width: 100,
height: 300
},
example: {
flexDirection: 'column',
justifyContent: 'space-around',
flex: 1,
height: 300,
},
one: {
backgroundColor: 'gray',
height: 50,
justifyContent: 'flex-start',
flexDirection: 'row',
},
two: {
backgroundColor: 'gray',
height: 50,
justifyContent: 'flex-end',
flexDirection: 'row',
},
three: {
backgroundColor: 'gray',
height: 50,
justifyContent: 'center',
flexDirection: 'row',
},
four: {
backgroundColor: 'gray',
height: 50,
justifyContent: 'space-between',
flexDirection: 'row',
},
five: {
backgroundColor: 'gray',
height: 50,
justifyContent: 'space-around',
flexDirection: 'row',
},
left: {
backgroundColor: 'green',
width: 50
},
middle: {
backgroundColor: 'black',
width: 50
},
right: {
backgroundColor: 'orange',
width: 50
}
});
- alignItems:
flex-start | flex-end | center | baseline | stretch
定義項(xiàng)目在交叉軸上如何對(duì)齊,可以把其想像成側(cè)軸(垂直于主軸)的“對(duì)齊方式”。
flex-start:交叉軸的起點(diǎn)對(duì)齊。
flex-end:交叉軸的終點(diǎn)對(duì)齊 。
center:交叉軸的中點(diǎn)對(duì)齊。
baseline:項(xiàng)目的第一行文字的基線對(duì)齊。
stretch(默認(rèn)值):如果項(xiàng)目未設(shè)置高度或設(shè)為auto,將占滿整個(gè)容器的高度。
const styles = StyleSheet.create({
contains: {
flexDirection: 'row',
justifyContent: 'space-around',
alignItems: 'center',
flex: 1
},
title: {
flexDirection: 'column',
justifyContent: 'space-around',
alignItems: 'center',
width: 100,
height: 300
},
example: {
flexDirection: 'column',
justifyContent: 'space-around',
flex: 1,
height: 300,
},
one: {
backgroundColor: 'gray',
height: 50,
justifyContent: 'flex-start',
flexDirection: 'row',
alignItems: 'flex-start'
},
two: {
backgroundColor: 'gray',
height: 50,
justifyContent: 'flex-end',
flexDirection: 'row',
alignItems: 'flex-end'
},
three: {
backgroundColor: 'gray',
height: 50,
justifyContent: 'center',
flexDirection: 'row',
alignItems: 'center'
},
four: {
backgroundColor: 'gray',
height: 50,
justifyContent: 'space-between',
flexDirection: 'row',
alignItems: 'baseline'
},
five: {
backgroundColor: 'gray',
height: 50,
justifyContent: 'space-around',
flexDirection: 'row',
alignItems: 'stretch'
},
left: {
backgroundColor: 'green',
width: 50,
height: 30
},
middle: {
backgroundColor: 'black',
width: 50,
height: 20
},
right: {
backgroundColor: 'orange',
width: 50,
height: 50
}
});
- flexWrap:
nowrap | wrap | wrap-reverse
默認(rèn)情況下,項(xiàng)目都排在一條線(又稱"軸線")上。flex-wrap屬性定義,如果一條軸線排不下,如何換行。
nowrap(默認(rèn)值):不換行。
wrap:換行,第一行在上方。
export default class meituanDemo extends Component {
render() {
return (
<View style={styles.contains}>
<View style={styles.title}>
<Text style={{height: 30, textAlign: 'center'}}>nowrap(默認(rèn)值)</Text>
<Text style={{height: 30, textAlign: 'center'}}>wrap</Text>
</View>
<View style={styles.example}>
<View style={styles.one}>
<View style={{backgroundColor: 'green',width: 50,height: 20,marginLeft:10,marginTop:2}}>
</View>
<View style={{backgroundColor: 'green',width: 50,height: 20,marginLeft:10,marginTop:2}}>
</View>
<View style={{backgroundColor: 'green',width: 50,height: 20,marginLeft:10,marginTop:2}}>
</View>
<View style={{backgroundColor: 'green',width: 50,height: 20,marginLeft:10,marginTop:2}}>
</View>
<View style={{backgroundColor: 'green',width: 50,height: 20,marginLeft:10,marginTop:2}}>
</View>
<View style={{backgroundColor: 'green',width: 50,height: 20,marginLeft:10,marginTop:2}}>
</View>
</View>
<View style={styles.two}>
<View style={{backgroundColor: 'green',width: 50,height: 20,marginLeft:10,marginTop:2}}>
</View>
<View style={{backgroundColor: 'green',width: 50,height: 20,marginLeft:10,marginTop:2}}>
</View>
<View style={{backgroundColor: 'green',width: 50,height: 20,marginLeft:10,marginTop:2}}>
</View>
<View style={{backgroundColor: 'green',width: 50,height: 20,marginLeft:10,marginTop:2}}>
</View>
<View style={{backgroundColor: 'green',width: 50,height: 20,marginLeft:10,marginTop:2}}>
</View>
<View style={{backgroundColor: 'green',width: 50,height: 20,marginLeft:10,marginTop:2}}>
</View>
</View>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
contains: {
flexDirection: 'row',
justifyContent: 'space-around',
alignItems: 'center',
flex: 1
},
title: {
flexDirection: 'column',
justifyContent: 'space-around',
alignItems: 'center',
width: 100,
height: 300
},
example: {
flexDirection: 'column',
justifyContent: 'space-around',
flex: 1,
height: 300,
},
one: {
backgroundColor: 'gray',
height: 50,
justifyContent: 'flex-start',
flexDirection: 'row',
flexWrap: 'nowrap'
},
two: {
backgroundColor: 'gray',
height: 50,
justifyContent: 'flex-end',
flexDirection: 'row',
flexWrap: 'wrap'
},
three: {
backgroundColor: 'gray',
height: 50,
justifyContent: 'center',
flexDirection: 'row',
},
four: {
backgroundColor: 'gray',
height: 50,
justifyContent: 'space-between',
flexDirection: 'row',
alignItems: 'baseline'
},
five: {
backgroundColor: 'gray',
height: 50,
justifyContent: 'space-around',
flexDirection: 'row',
alignItems: 'stretch'
},
left: {
backgroundColor: 'green',
width: 50,
height: 30
},
middle: {
backgroundColor: 'black',
width: 50,
height: 20
},
right: {
backgroundColor: 'orange',
width: 50,
height: 50
}
});
-
元素屬性
- flex
flex
屬性是flex-grow
,flex-shrink
和flex-basis
的簡寫,默認(rèn)值為0 1 auto
。后兩個(gè)屬性可選。 默認(rèn)值為“0 1 auto
”。
寬度 = 彈性寬度 * ( flexGrow / sum( flexGorw ) )
- alignSelf: “
auto | flex-start | flex-end | center | baseline | stretch
”
align-self
屬性允許單個(gè)項(xiàng)目有與其他項(xiàng)目不一樣的對(duì)齊方式,可覆蓋align-items
屬性。默認(rèn)值為auto
,表示繼承父元素的align-items
屬性,如果沒有父元素,則等同于stretch
。
- flex
四、在React Native 中使用FlexlBox布局
- 獲取當(dāng)前屏幕的寬度、高度和scale
var {width, height, scale} = Dimensions.get('window')
export default class meituanDemo extends Component {
render() {
return (
<View style={styles.contains}>
<Text style={styles.welcome}>
當(dāng)前屏幕的寬度:{width + '\n'}
當(dāng)前屏幕的高度:{height + '\n'}
當(dāng)前屏幕的scale:{scale + '\n'}
</Text>
</View>
);
}
}
const styles = StyleSheet.create({
contains: {
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
flex: 1,
},
welcome: {
textAlign: 'center',
flex: 1
}
});
- 水平居中,垂直居中和水平垂直居中
export default class meituanDemo extends Component {
render() {
return (
<View style={styles.contains}>
<View style={styles.title}>
<Text style={{height: 30, textAlign: 'center'}}>水平居中</Text>
<Text style={{height: 30, textAlign: 'center'}}>垂直居中</Text>
<Text style={{height: 30, textAlign: 'center'}}>水平垂直居中</Text>
</View>
<View style={styles.example}>
<View style={styles.one}>
<Image source={{uri:'http://farm4.staticflickr.com/3795/9269794168_3ac58fc15c_b.jpg'}} style={{width:30,height:30}} />
</View>
<View style={styles.two}>
<Image source={{uri:'http://farm4.staticflickr.com/3795/9269794168_3ac58fc15c_b.jpg'}} style={{width:30,height:30}} />
</View>
<View style={styles.three}>
<Image source={{uri:'http://farm4.staticflickr.com/3795/9269794168_3ac58fc15c_b.jpg'}} style={{width:30,height:30}} />
</View>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
contains: {
flexDirection: 'row',
justifyContent: 'space-around',
alignItems: 'center',
flex: 1
},
title: {
flexDirection: 'column',
justifyContent: 'space-around',
alignItems: 'center',
width: 100,
height: 300
},
example: {
flexDirection: 'column',
justifyContent: 'space-around',
flex: 1,
height: 300,
},
one: {
backgroundColor: 'gray',
height: 50,
alignItems: 'center'
},
two: {
backgroundColor: 'gray',
height: 50,
justifyContent: 'center',
},
three: {
backgroundColor: 'gray',
height: 50,
alignItems: 'center',
justifyContent: 'center',
}
});
備注:一旦設(shè)置
alignItems
屬性之后,組件的大小包裹隨著內(nèi)容的尺寸;此外水平居中和垂直居中還要結(jié)合FlexDirection
進(jìn)行判斷。