Nodejs 一度将前端JS 推到了服务器端,而15年FB的React-Native RN再一次将JS 推到了移动端的开发浪潮中。RN的优势这里不再重复,它是我们这些习惯了服务端、web端开发,而又不想去花太多时间掌握Android、IOS移动端原生开发的人员的福音,可以说是我们通向全栈工程师的快速捷径!于是乎最近开始学习React-Native,并将学习中的一些点滴记录下来。

  网上关于RN的资料不少了,首先是环境配置,不一定非得Mac 下。我是基于Windows开发的,Windows下环境如何配置大家可以参考这篇文章 Windows下RN环境搭配。准备好开发环境之后就是具体的开发知识学习了。首先是RN控件的学习,这里我们先学习RN的控件布局。

  RN的布局采用的是CSS+Flexbox布局方式。我们初始化使用 命令初始化一个名叫reactNative的项目,命令如下:react-native init reactNative。等待完成之后,我们使用WebStorm打开项目,在项目里面的index.android.js文件中插入如下代码:

 import React, {Component} from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View
} from 'react-native'; var App = React.createClass({
render: function () {
return (
<View style={styles.main}>
<View style={styles.view1}>
<Text style={styles.text}>Test1</Text>
<Text style={styles.text}>Test2</Text>
<Text style={styles.text}>Test3</Text>
</View>
<View style={styles.view2}>
<Text style={styles.text}>Test4</Text>
<Text style={styles.text}>Test5</Text>
<Text style={styles.text}>Test6</Text>
<Text style={styles.text}>Test7</Text>
<Text style={styles.text}>Test8</Text>
<Text style={styles.text}>Test9</Text>
<Text style={styles.text}>Test10</Text>
</View>
</View>
);
}
})
var styles = StyleSheet.create({
main: {
flex: 1, borderColor: 'blue', margin: 5, borderWidth: 1
},
view1: {
borderColor: 'red', borderWidth: 1, flexDirection: 'column', height: 150
},
view2: {
borderColor: 'red', borderWidth: 1, flexDirection: 'row'
},
text: {
fontSize: 14, margin: 10
}
})
AppRegistry.registerComponent('reactNative', ()=>App);

  编辑完代码之后,我们使用windows command 进入项目的目录,然后使用命令:react-native start启动项目,然后启动模拟器,接着使用命令:react-native run-android。就可以在模拟器中看到如下效果!

这里结合代码,我们可以看出:最外层的View我们使用的是样式style.main,采用的flex:1,borderColor:’blue’ 就是我们图片中最外层的蓝色边框视图。flex值大于0是表示控件是可以伸缩的,由于没有其他视图和这里的最外层View视图竞争空间,因此它填充满了我们这个手机屏幕。然后里面有2个子View视图。

第一个视图使用样式style.view1 它的边框是红色,采用的flexDirection:’column’纵向伸缩,因此它里面的三个Text控件都是从上往下依次排列的。

第二个视图使用样式style.view2 它的边框也是红色,采用的flexDirection:’row’横向伸缩,因此它里面的7个Text控件都是从左向右依次排列的。(最后一个Text超出了手机宽度边界,未显示完整。)

由此我们可以看出:在RN中flexbox的flexDirection有两个字column和row默认为column值。当设置为column时控件按照纵向依次排列布局,当设置为row时控件按照横向依次排列布局;

前面我们说了最外层的View设置flex=1是因为没有其他控件和它竞争空间,因此它填充满了我们整个手机屏幕。这里我们修改一下样式表styles向其中添加几个样式,并将它们应用到View1中的3个Text中,修改代码如下:

 var App = React.createClass({
render: function () {
return (
<View style={styles.main}>
<View style={styles.view1}>
<Text style={[styles.text,styles.row1]}>Test1</Text>
<Text style={[styles.text,styles.row2]}>Test2</Text>
<Text style={[styles.text,styles.row3]}>Test3</Text>
</View>
<View style={styles.view2}>
<Text style={styles.text}>Test4</Text>
<Text style={styles.text}>Test5</Text>
<Text style={styles.text}>Test6</Text>
<Text style={styles.text}>Test7</Text>
<Text style={styles.text}>Test8</Text>
<Text style={styles.text}>Test9</Text>
<Text style={styles.text}>Test10</Text>
</View>
</View>
);
}
})
var styles = StyleSheet.create({
main: {
flex: 1, borderColor: 'blue', margin: 5, borderWidth: 1
},
view1: {
borderColor: 'red', borderWidth: 1, flexDirection: 'column', height: 150
},
view2: {
borderColor: 'red', borderWidth: 1, flexDirection: 'row'
},
text: {
fontSize: 14, margin: 10
},
row1: {flex: 3},
row2: {flex: 2},
row3: {flex: 1}
})

   这里我们将View1中的三个Text控件的flext值分别设置为3,2,1,然后我们在手机中看的效果如下图:我们可以看出三个Text控件的高度分别为3:2:1占满了我们的View1的高度,这也证实了我们前面的结论。

  

  然后我们看一下alignSelf布局,alignSelf的对齐方式主要有四种:flex-start、 flex-end、 center、  auto、 stretch。我们添加如下代码:

 <View style={styles.view3}>
<View style={[styles.left,{width:100,height:40, borderWidth:1,borderColor:'silver'}]}><Text>left</Text></View>
<View style={[styles.center,{width:100,height:40,borderWidth:1,borderColor:'silver'}]}><Text>center</Text></View>
<View style={[styles.right,{width:100,height:40,borderWidth:1,borderColor:'silver'}]}><Text>right</Text></View>
<View style={[styles.default,{width:100, height:40,borderWidth:1,borderColor:'silver'}]}><Text>default</Text></View>
</View> styles:
view3: {flex: 1, margin: 5, borderColor: 'red', borderWidth: 1},
left: {alignSelf: 'flex-start'},
center: {alignSelf: 'center'},
right: {alignSelf: 'flex-end'},
default: {alignSelf: 'auto'},
可以看到如下的效果:

  然后还有alignItems、justifyContent它们分别是水平居中、垂直居中。它们的用法如下,我们添加如下代码:
 <View style={[styles.view3,{alignItems:'center',justifyContent:'center'}]}>
<View style={{width:120,height:30, borderWidth:1,borderColor:'silver'}}>
<Text>水平垂直居中</Text>
</View>
</View>
运行之后可以看到如下效果:

以上就是RN的CSS和flexbox布局的简单学习。

最新文章

  1. 数据库(SQL SERVER)常用知识点
  2. Android蓝牙实例(和单片机蓝牙模块通信)
  3. AngularJS快速入门指南04:指令
  4. Android 屏幕滑动事件
  5. mysql 导出表结构
  6. Noah的学习笔记之Python篇:命令行解析
  7. 看了看 #ifndef 和#pragma once 的区别
  8. 使用python写一个简单的C段扫
  9. Dynamics CRM 权限整理二
  10. idea运行多模块的maven项目,工作目录不一致的问题
  11. Spring MVC的文件上传和下载
  12. 【js课设】电子画板01
  13. lr_场景设计之组场景、nmon监控
  14. Oracle通过ROWID删除表中重复记录
  15. python数据结构与算法第十七天【概率算法】
  16. 电子产品使用感受之—我的iPad Pro坏了。。。
  17. chrome视频播放加速
  18. Python自动化开发 - RESTful API
  19. java程序员必须要学会的linux命令总结
  20. datagrid---columns列

热门文章

  1. iOS开发——底层OC篇&amp;运行时常用
  2. Boltzmann机
  3. next_permutation 和 一个不成功的案例
  4. Kali Linux下破解WIFI密码挂载usb无线网卡的方法
  5. 《ASP.NET MVC4 WEB编程》学习笔记------ViewBag、ViewData和TempData的使用和区别
  6. Google Code Jam 2014 Round 1B Problem B
  7. Cocos2d 初学基本知识
  8. iOS 和 Android 触摸事件传递
  9. 对Java中字符串的进一步理解
  10. 【JAVA、C++】LeetCode 006 ZigZag Conversion