React Native-4.React Native布局属性练习之flexBox模型实战

前端之家收集整理的这篇文章主要介绍了React Native-4.React Native布局属性练习之flexBox模型实战前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

废话不多说,学以致用!
代码

使用react native init 创建的项目中,在index.ios.js中编写的代码

wxsPrj 是我的工程名字,如果你的工程名字是xxx,那么请把下列代码中出现的所有wxsPrj 换成你的xxx 然后覆盖原来index.ios.js 中的所有代码,运行一下,得到的效果如下图所示:

  1. 'use strict';
  2.  
  3. var React = require('react-native');
  4. var {
  5. AppRegistry,StyleSheet,Text,View,} = React;
  6.  
  7. var BoxStyles = StyleSheet.create({
  8. "height50": {
  9. height : 50,},"height400": {
  10. height : 400,"width400" : {
  11. width : 400,"bgred" : {
  12. backgroundColor : "#6AC5AC","Box" : {
  13. flexDirection : "column",flex: 1,position : "relative","centerView" : {
  14. flexDirection: "row",flex : 1,justifyContent : "space-between","label" : {
  15. top: 0,left: 0,paddingTop : 0,paddingRight: 3,paddingBottom : 3,position : "absolute",backgroundColor : "#FDC72F","top" : {
  16. justifyContent : "center",alignItems : "center","bottom" : {
  17. justifyContent: "center","right" : {
  18. justifyContent:"space-around",width : 50,"left" : {
  19. justifyContent: "space-around",width: 50,alignItems: "center","margginBox" : {
  20. "position" : "absolute","top" : 100,"paddingLeft" : 7,"paddingRight" : 7,})
  21.  
  22. var Box = React.createClass({
  23. render:function(){
  24. return(
  25. <View style = {[BoxStyles.Box,BoxStyles[this.props.width],BoxStyles[this.props.height]]}>
  26. <View style = {[BoxStyles.top,BoxStyles.height50,BoxStyles[this.props.classBg]]}>
  27. <Text>top</Text>
  28. </View>
  29.  
  30. <View style={[BoxStyles[this.props.childName]]}>
  31. <View style={[BoxStyles.left,BoxStyles[this.props.classBg]]}>
  32. <Text>left</Text>
  33. </View>
  34. <View style={[BoxStyles.right,BoxStyles[this.props.classBg]]}>
  35. <Text>right</Text>
  36. </View>
  37. </View>
  38.  
  39. <View style={[BoxStyles.bottom,BoxStyles[this.props.classBg]]}>
  40. <Text>bottom</Text>
  41. </View>
  42.  
  43. <View style={[BoxStyles.label]}>
  44. <Text>{this.props.BoxName}</Text>
  45. </View>
  46. </View>
  47. )
  48. }
  49.  
  50. })
  51. var MargginBox = React.createClass({
  52. render : function(){
  53. return (
  54. <View style={[BoxStyles.margginBox]}>
  55. <Box childName="centerView" height="height400" width="width400" BoxName="margin" classBg="bgred">
  56. {this.props.children}
  57. </Box>
  58. </View>
  59. )
  60. }
  61.  
  62. })
  63.  
  64. var wxsPrj = React.createClass({
  65. render: function() {
  66. return (
  67. <MargginBox></MargginBox>
  68. )}
  69. })
  70.  
  71. AppRegistry.registerComponent('wxsPrj',() => wxsPrj);

代码详解:

BoxStyles

var BoxStyles = StyleSheet.create
创建一个样式列表,里边是我们所能用到的一些控件的样式,就像我们在iOS中编写一个.h文件一样,统一的去管理一些常用或常改的变量一样。内容含义观看我上几篇对应的博客会有详细的理解。

Box

var Box = React.createClass
这里创建一个组件,这个组建就是盒子模型的详细实现过程,各种view之间的嵌套和样式的嵌套,也不难理解。
这里边使用到了类似this.props.classBg,this.props.BoxNam的写法,我们来说明一下:

this

this我们完全可以等同的理解为iOS 中的self指针。

props

这个变量,它是组件中一个自动生成的类似数组的变量,它是一个对象,用来组件接收外面传进来的参数,组件内部是不允许修改自己的props,只能通过父组件来修改
这个变量是在组件初始化阶段就被自动创建的。你大可认为它是一个数组,外边传进来的参数全部存放在里边。记录组件的属性

this.props.classBg

它的意思就是说我在props中取得classBg这个属性,那么这个属性我们是没有定义,那么从哪里来的,正如上边所说,props记录的是外边传进来的属性。那么我们在使用Box组件的时候需要给他传一个classBg的参数。

MargginBox

这个才是我们最中想要的组件,我们不难发现,我们在其中使用了Box组件,并传入了一些参数,这个结合上边的解释,想必大家都明白了。。

猜你在找的React相关文章