Flexbox
Flexbox 是一种布局模式,用于在页面上排列元素,使得当页面布局必须适应不同的屏幕尺寸和不同的显示设备时,元素的行为可预测。默认情况下,flexbox 会在列中排列子项。但你可以使用 flexDirection: 'row'
将其更改为行。
flexDirection
const Direction = (props)=>{
return (
<View style={styles.container}>
<Box/>
<Box/>
<Box/>
<View style={{flexDirection:'row'}}>
<Box/>
<Box/>
<Box/>
</View>
</View>
)
}
const styles = StyleSheet.create({
container: {
flex:1,
backgroundColor: '#AED581',
}
});
对齐轴
const AlignmentAxis = (props)=>{
return (
<View style={styles.container}>
<Box />
<View style={{flex:1, alignItems:'flex-end', justifyContent:'flex-end'}}>
<Box />
<Box />
</View>
<Box />
</View>
)
}
const styles = StyleSheet.create({
container: {
flex:1,
backgroundColor: `#69B8CC`,
},
text:{
color: 'white',
textAlign:'center'
}
});
对准
const Alignment = (props)=>{
return (
<View style={styles.container}>
<Box/>
<View style={{alignItems:'center'}}>
<Box/>
<View style={{flexDirection:'row'}}>
<Box/>
<Box/>
<Box/>
</View>
<Box/>
</View>
<Box/>
</View>
)
}
const styles = StyleSheet.create({
container: {
flex:1,
backgroundColor: `#69B8CC`,
},
text:{
color: 'white',
textAlign:'center'
}
});
弹性尺寸
const FlexSize = (props)=>{
return (
<View style={styles.container}>
<View style={{flex:0.1}}>
<Box style={{flex:0.7}}/>
<Box style={{backgroundColor: 'yellow'}}/>
<Box/>
<Box style={{flex:0.3, backgroundColor: 'yellow'}}/>
</View>
<View style={{flex:0.1}}>
<Box style={{flex:1}}/>
<Box style={{backgroundColor: 'yellow'}}/>
<Box/>
<Box style={{flex:1, backgroundColor: 'yellow'}}/>
</View>
</View>
)
}
const styles = StyleSheet.create({
container: {
flex:1,
flexDirection:'row',
backgroundColor: colors[1],
},
});
更多关于 Facebook 的 Flexbox 的实施在这里 。