React Native-3.React Native中的主要布局属性介绍和练习
2016-01-20 13:17
609 查看
我们搞出些事情
我们上两节学习了CSS的伸缩属性标签的作用和效果实现,这一节我们顺势学习一下在React Native 中是使用flexboxReact Native 中的主要flexbox属性介绍
alignItemsalignSelf
flex
flexDirection
flexWrap
justifycontent
Just Do it
1、alignItems
该属性的用法和欠扁的align-items,区别在于写法使用驼峰拼写法,语法:alignItems : flex-start | flex-end | center | strech
注: 因为前两张对这些属性和参数的功能用法都有详细的解释,本节不再做展示
2、alignSelf
该属性的用法同赏遍的align-self,区别在于它需要使用驼峰拼写法,语法:
alignSelf: auto | flex-start | flex-end | center | stretch
3、flex
该属性的用法同上一章节的flex,语法:felx : number
4、flexDirection
该属性的用法同上面的flex-direction,区别在于React Native 中默认的是column,其语法为:
flexDirection: row | cloumn
5、flexWrap
该属性的用法同上面的flex-wrap,区别在于需要使用驼峰拼写法,语法:
flexWrap: wrap | nowrap
6、justifyContent
该属性的用法同上边的justify-content,区别在于需要使用驼峰拼写法,语法:
justifyContent: flex-start | flex-end | center | space-around | space-between
光说不练假把式
我们来用学习CSS的经典模型:盒子模型,来联系我们学习的属性实现效果:
ps: 自行忽略信号和时间。。。。
代码:
'use strict'; var React = require('react-native'); var { AppRegistry, StyleSheet, Text, View, } = React; var wxsPrj = React.createClass({ render: function() { return ( <View style = {styles.boxRect}> <View style = {styles.top}> <Text style = {styles.myTextColor}> top </Text> </View> <View style = {styles.centerView}> <View style = {[styles.left,styles.height100,styles.width50]}> <Text style = {styles.myTextColor}> left </Text> </View> <View style = {[styles.right,styles.height100,styles.width50]}> <Text style={styles.myTextColor}> right </Text> </View> </View> <View style = {styles.bottom}> <Text style = {styles.myTextColor}> bottom </Text> </View> </View> ); } }); var styles = StyleSheet.create({ myTextColor: { height:20, width:50, alignSelf:'center', textAlign:'center', color: '#ffffff', }, boxRect: { backgroundColor: '#555555', flexDirection:'column', flexWrap:'nowrap', justifyContent:'flex-start', width:200, height:200, }, top: { backgroundColor: '#666666', justifyContent: 'center', flexDirection:'row', height:50, }, bottom: { backgroundColor: '#666666', justifyContent: 'center', height:50, }, right: { backgroundColor: '#666666', justifyContent: 'center', flexDirection:'row', }, left: { backgroundColor: '#666666', justifyContent: 'center', flexDirection:'row', }, centerView: { justifyContent:'space-between', flexDirection:'row', }, height50: { height: 50, }, width50: { width: 50, }, height100:{ height:100, } }); AppRegistry.registerComponent('wxsPrj', () => wxsPrj);
运行后的效果:
最后总是得说点什么装一下。。。
总结一下CSS中标签的书写格式和React Native中标签的书写格式的区别:- CSS以分号(;)结尾 ,RN 以逗号(,)结尾;
- CSS中key,value都不加引号,RN中属于JavaScript对象 ,如果value为字符串,需要用引号引起来,并且key值中间不能出现“-“,一定要转为驼峰命名方式。
- CSS中,value值为数字时,需要带单位,RN中则不用带。
相关文章推荐
- ReactJS结合ES6入门Template
- React-Native post和get请求
- React-Native获取文本框的值
- Reactor Pattern Explained - Part 1, 2 and 3
- react native 入门探索
- 在Windows上将ReactNative集成到现有的Android项目
- Reactor模式
- 6、React中的表单
- you are trying to reader the global image variable as a react element.you probably forgot to require
- React Native入门——布局实践:开发京东客户端首页(一)
- React Native HelloWorld
- 获取元素的class react
- React Native入门之环境配置
- React Native For Android初探-问题小结
- 【React Native开发】React Native控件之View视图讲解(7)
- React学习入门实例
- react(1)-broswerify+reactify+gulp搭建react的workflow+React入门资料集合
- react native之API学习
- 5、Reactjs中组件之间的通信
- react资料汇总