React Native项目初窥(一)
上一篇介绍了React Native开发环境搭建 WebStorm配置当使用 WebStorm 打开我们init下来的项目,会报各种错误 React Native项目初窥项目结构 index.android.js
1、导入部分//导入React
import React,{ Component } from 'react';
//导入各组件
import {
AppRegistry,//注册器
StyleSheet,//样式表
Text,View,//基本的视图容器
} from 'react-native';
AppRegistry:是JS运行所有React Native应用的入口。应用的根组件应当通过AppRegistry.registerComponent方法注册自己,然后原生系统才可以加载应用的代码包并且在启动完成之后通过调用AppRegistry.runApplication来真正运行应用。 StyleSheet:提供了一种类似CSS样式表的抽象 2、视图部分—使用样式class MyProject extends Component {
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Welcome to React Native!
</Text>
<Text style={styles.instructions}>
To get started,edit index.android.js
</Text>
<Text style={styles.instructions}>
Shake or press menu button for dev menu
</Text>
</View>
);
}
}
继承Component,重写render()方法,为组件指定样式 View:作为创建UI时最基础的组件,是一个支持Flexbox布局、样式、一些触摸处理、和一些无障碍功能的容器,并且它可以放到其它的视图里,也可以有任意多个任意类型的子视图。 3、样式部分—声明样式const styles = StyleSheet.create({
container: {
flex: 1,justifyContent: 'center',alignItems: 'center',backgroundColor: '#F5FCFF',},welcome: {
fontSize: 20,textAlign: 'center',margin: 10,instructions: {
textAlign: 'center',color: '#333333',marginBottom: 5,});
这里使用了Flex的属性—>弹性盒(Flexbox) 样式支持的属性
许多属性不明含义 4、注册部分AppRegistry.registerComponent('MyProject',() => MyProject);
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |