index.jsreact
// 从 Reactnative 的包中,导入 AppRegistry 组件,它的做用,就是注册项目首页的 import { AppRegistry } from 'react-native'; import App from './App'; // 导入本身的组件页面 import MyHomePage from './MyHomePage.js' // 当使用 AppRegistry 注册项目的时候,方法中的第一个参数,不要改,不然项目就废了 // 第二个参数,表示要把哪一个页面注册为项目的首页 AppRegistry.registerComponent('rn_douban', () => App);
MyHomePage.jsandroid
// 在 RN 中只能使用 .js 做为 组件的后缀名,不能使用 .jsx import React, { Component } from 'react' // View 组件负责布局,就比如 网页中的 div 元素 import { View, Text } from 'react-native' export default class MyHomePage extends Component { // constructor 通常也推荐都写出来 constructor(props) { super(props) this.state = {} } // 必须有 render 函数 render() { // 1, 在 RN 中,不能使用 网页中的 全部标签,像 div , p , img不能用 // 2. 若是想要实现布局, RN 提供了 View 的组件,来实现布局;RN 提供了一系列基础的组件,来方便程序员的开发,若是想要使用这些组件,只需 按需 把 组件 从 'react-native' 中导入便可 return <View> {/* 在 RN 中,全部的文本,必须使用 RN 提供的 Text 组件进行包裹;不然会报错 */} <Text>123456~~~~~</Text> </View> } }
app.jsios
/** * Sample React Native App * https://github.com/facebook/react-native * @flow */ // 导入 React 基础包,这个包,做用是建立 组件 import React, { Component } from 'react'; // 从 react-native 中导入系统开发须要的包 import { Platform, // 用来提供平台检测功能的 StyleSheet, // 样式相关的组件,专门用来建立样式的 Text, // 文本节点,全部文本必须放到这个里面 View, // 用来布局的,至关于 div TextInput, // 文本框组件 Image, // 图片组件 Button, // 按钮组件 ActivityIndicator, // 加载中的 loading 效果小圆圈 ScrollView, // 滚动组件(默认,若是一个RN的页面很是长,超出了屏幕高度,这时候,不会像网页中那样自动提供滚动条,若是须要让页面实现滚动的话,须要使用 ScrollView 把页面包裹起来) } from 'react-native'; const instructions = Platform.select({ ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu', android: 'Double tap R on your keyboard to reload,\n' + 'Shake or press menu button for dev menu', }); // 这是 TS(TypeScript) 的语法 export default class App extends Component { render() { return ( <View style={styles.container}> {/* 若是使用 animating 隐藏 loading效果,只是让他不可见了,可是区域还在 */} <ScrollView style={{width: '100%'}}> <ActivityIndicator color="red" size="large" animating={true}></ActivityIndicator> <Text style={styles.welcome}> Welcome to React Native! </Text> <Text style={styles.instructions}> To get started, edit App.js </Text> <Text style={styles.instructions}> {instructions} </Text> <TextInput style={styles.inputStyle} defaultValue="哈哈,今天总有人在后面说话" secureTextEntry={true}></TextInput> {/* 引入本地的图片资源 */} <Image source={require('./images/1.jpg')}></Image> <Image source={require('./images/1.jpg')}></Image> <Image source={require('./images/1.jpg')}></Image> <Image source={require('./images/1.jpg')}></Image> {/* 引入网络中的图片资源,除了要指定 一个 uri 资源路径以外,还须要 为这张网络图片,指定宽和高,不然显示不出来; */} <Image source={{ uri: 'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=358796479,4085336161&fm=200&gp=0.jpg' }} style={{ width: 200, height: 160 }}></Image> {/* 在 Button 按钮中, title 属性 和 onPress 属性是必须的 */} {/* onPress 表示点击按钮,要触发的操做 */} <Button title="这是一个按钮" onPress={() => { console.warn('123') }}></Button> </ScrollView> </View> ); } } // 使用 StyleSheet.create 建立样式表对象,能够为 create 传递 一个配置对象,这个对象,就是要建立的样式 const styles = StyleSheet.create({ container: { flex: 1, // 在RN中,主轴默认是纵向的 justifyContent: 'flex-start', // 一些 文本类型的 样式值,须要使用 引号包裹起来 alignItems: 'flex-start', backgroundColor: '#F5FCFF', }, welcome: { fontSize: 20, // 长度单位的 px 值能够省略,由于 RN 默认就是以 px 做为单位的 textAlign: 'center', margin: 10, }, instructions: { textAlign: 'center', color: '#333333', marginBottom: 5, }, inputStyle: { width: '100%' } }); // 总结:若是要定义一个基本的 RN 页面:须要的步骤: // 1. 导入 react 包,来建立组件 // 2. 导入 react-native 包,来提供基础的开发组件 // 3. 从 react-naitve 中,能够导入 StyleSheet 的组件,用它 的 create 方法,能够建立样式对象 // 4. 使用 react 基础的语法,建立出来的组件,就是一个 合法的 RN 组件页面;若是想要在页面中,放置一些合法的 页面元素,只能使用 RN 库提供的 固有组件;不能使用 网页中的 任何 元素;