React Native开发之FlexBox代码+图解
来自Leo的原创博客,转载请著名出处 我的stackoverflow 前言FlexBox布局是React Native的布局核心,鉴于自己对FlexBox还有很多概念不太清楚,这篇文章就当成是总结,并且分享出来给大家。
本文参考文章如下
当然,React Native官网也有FlexBox相关的文档,不过讲解的内容不多。 !!!!React Native的迭代更新很快,所以如果有读者发现了本文代码不适配新版本了,欢迎评论,我会及时修改。 Getting Startreact-native init LearnFlexBox
运行 cd LearnFlexBox/
react-native run-ios
会看到默认的截图 由于模拟器截图实在太宽,所以本文把Demo范围限定在一个小的范围内 render() {
return (
<View style={styles.container}>
<View style={styles.exampleContainer}>
</View>
</View>
);
}
这里的exampleContainer风格如下 exampleContainer:{ width:320,height:200,backgroundColor:'gray' }
Container和Item
flexDirection这个Container属性决定了按照哪个方向来布局Item,默认从上到下 exampleItem:{ width:30,height:30,backgroundColor:'#27E6E2' }
然后,在添加三个子视图 <View style={styles.exampleContainer}>
<View style={styles.exampleItem}></View>
<View style={styles.exampleItem}></View>
<View style={styles.exampleItem}></View>
</View>
然后,修改 exampleContainer:{ width:320,backgroundColor:'gray',flexDirection:'column' },
当flexDirection设置为 注意:React Native的Flexbox目前不支持row-reverse和column-reverse alignItems-垂直轴上的位置关系有四个值 'flex-start','flex-end','center','stretch'
示例代码如下 <View style={{width:320,height:200,backgroundColor:'gray',alignItems:'stretch',flexDirection:'row'}}> <View style={{width:50,backgroundColor:'green'}}></View> <View style={{width:50,backgroundColor:'blue'}}></View> <View style={{width:50,height:50,backgroundColor:'red'}}></View> <View style={{width:50,height:60,backgroundColor:'orange'}}></View> </View>
<View style={{width:320,backgroundColor:'orange'}}></View> </View>
效果 alignSelf五个值,当Item有这个属性的时候,会优先读取item的alignSelf来布局,也就是说会覆盖Container的alignItems。例如 <View style={{width:320,alignItems:'center',height:30,backgroundColor:'green',alignSelf:'flex-end'}}></View> <View style={{width:50,height:40,backgroundColor:'blue',alignSelf:'flex-start'}}></View> <View style={{width:50,alignSelf:'stretch',alignSelf:'auto',backgroundColor:'orange'}}></View> </View>
效果 justifyContent-水平轴上的位置关系五个值 'flex-start','space-between','space-around'
同样,我们还是举个例子,来看看实际效果 <View style={{width:320,justifyContent:'space-around',backgroundColor:'orange'}}></View> </View>
flex-占据剩余空间的权重例如,如下,不设置宽度,高度一样,三个item,flex都是1,那么每个item的宽度占据1/3 <View style={{width:320,flexDirection:'row'}}> <View style={{flex:1,backgroundColor:'green'}}></View> <View style={{flex:1,backgroundColor:'blue'}}></View> <View style={{flex:1,backgroundColor:'red'}}></View> </View>
效果 flexWrap-决定在flex的方向上填满后是否换行两个值,默认不换行 'wrap','nowrap'
代码 <View style={{width:320,flexDirection:'column',flexWrap:'wrap'}}> <View style={{width:50,backgroundColor:'orange'}}></View> <View style={{width:50,height:70,backgroundColor:'pink'}}></View> </View>
换行效果 不换行效果 top/bottom/left/right这个比较直观,距离上下左右的距离 <View style={{width:320,flexDirection:'row'}}> <View style={{top:10,left:10,width:30,backgroundColor:'green'}}></View> </View>
效果 padding相关与Padding相关的一共有以下几个
例如,一个100*100View在没有padding的时候 <View style={{width:320,alignItems:'flex-start',paddingLeft:20,paddingTop:10}}> <View style={{width:100,height:100,backgroundColor:'pink'}}></View> </View>
效果 <View style={{width:320,backgroundColor:'pink'}}></View> </View>
效果 Border相关几个相关的属性
举个例子 <View style={{width:320,justifyContent:'center'}}> <View style={{width:50,backgroundColor:'pink',borderBottomWidth:3,borderColor:'white'}}></View> </View>
margin相关于margin相关的一共有以下几个属性
在不设置margin的情况下 <View style={{width:320,justifyContent:'center',flexDirection:'row'}}> <View style={{width:30,backgroundColor:'pink' }}></View> <View style={{width:30,backgroundColor:'blue'}}></View> <View style={{width:30,backgroundColor:'green'}}></View> </View>
效果 <View style={{width:320,marginLeft:10,marginRight:20}}></View> <View style={{width:30,backgroundColor:'green'}}></View> </View>
效果 后续由于没有Web的开发经验,所以React Native的博客更新较慢,这个系列应该会一直更新下去。如果发现问题,欢迎评论,我会及时修正。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- FLEX学习-1环境搭建
- ajax – Googlebot如何知道网络服务器在请求`?_escaped_fr
- sqlite没有lib文件 如何手动生成lib文件
- iis-7.5 – 如何让Deploy.cmd填充Parameters.xml中的替换字
- objective-c – 在tvOS中呈现视图控制器
- objective-c – 为什么Xcode会自动创建带下划线的变量?
- c – int int(0)in int * pi = new int(0);?
- c# – 如何将代码中创建的图表添加到渲染的html页面?
- 操作 sqlite数据库 Warning: there is at least one open r
- 关于sqlite的一些语句