1.Main.js

  1. /**
  2. * 主页面
  3. */
  4. import React, { Component } from 'react';
  5. import {
  6. StyleSheet,
  7. Text,
  8. View,
  9. Image,
  10. Platform, //判断当前运行的系统
  11. } from 'react-native';
  12.  
  13. /*=============导入外部组件类==============*/
  14. import TabNavigator from 'react-native-tab-navigator';
  15. import CustomerComponents, { Navigator } from 'react-native-deprecated-custom-components';
  16.  
  17. // 引入外部的组件(此处注意是相当于了项目根目录)
  18. var Home = require('../Component/Home');
  19. var Message = require('../Component/Message');
  20. var Find = require('../Component/Find');
  21. var Mine = require('../Component/Mine');
  22.  
  23. // ES5
  24. var Main = React.createClass({
  25. // 初始化函数(变量是可以改变的,充当状态机的角色)
  26. getInitialState(){
  27. return{
  28. selectedTab:'home' // 默认选中的tabBar
  29. }
  30. },
  31.  
  32. render() {
  33. return (
  34. <TabNavigator>
  35. {/*--首页--*/}
  36. {this.renderTabBarItem('首页','icon_tabbar_home','icon_tabbar_home_selected','home','首页',Home,1)}
  37. {/*--消息--*/}
  38. {this.renderTabBarItem('消息','icon_tabbar_message','icon_tabbar_message_selected','message','消息',Message,2)}
  39. {/*--发现--*/}
  40. {this.renderTabBarItem('发现','icon_tabbar_find','icon_tabbar_find_selected','find','发现',Find)}
  41. {/*--我的--*/}
  42. {this.renderTabBarItem('我的','icon_tabbar_mine','icon_tabbar_mine_selected','mine','我的',Mine)}
  43. </TabNavigator>
  44. );
  45. },
  46.  
  47. // 封装tabBarItem
  48. renderTabBarItem(title,iconName,selectedIconName,selectedTab,componentName,component,badgeText){
  49. return(
  50. <TabNavigator.Item
  51. title={title}
  52. renderIcon={() => <Image source={{uri:iconName}} style={styles.iconStyle} />}
  53. renderSelectedIcon={() => <Image source={{uri:selectedIconName}} style={styles.iconStyle} />}
  54. selected={this.state.selectedTab === selectedTab}
  55. onPress={() => this.setState({ selectedTab: selectedTab })}
  56. selectedTitleStyle={styles.selectedTitleStyle} //tabBarItem选中的文字样式
  57. badgeText={badgeText}
  58. >
  59. <Navigator
  60. initialRoute={{name: componentName, component:component}}
  61. configureScene={()=>{
  62. return Navigator.SceneConfigs.PushFromRight;
  63. }}
  64. renderScene={(route, navigator) =>{
  65. let Component = route.component;
  66. return <Component {...route.passProps} navigator={navigator} />
  67. }}
  68. />
  69. </TabNavigator.Item>
  70. )
  71. }
  72. });
  73.  
  74. const styles = StyleSheet.create({
  75. // icon默认样式
  76. iconStyle:{
  77. width: Platform.OS === 'ios' ? 30 : 25,
  78. height:Platform.OS === 'ios' ? 30 : 25,
  79. },
  80. // tabBarItem选中的文字样式
  81. selectedTitleStyle:{
  82. color: 'rgba(212,97,0,1)',
  83. }
  84. });
  85.  
  86. // 输出
  87. module.exports = Main;

简化代码

2.Home.js

  1. /**
  2. * 首页
  3. */
  4. import React, { Component } from 'react';
  5. import {
  6. AppRegistry,
  7. StyleSheet,
  8. Text,
  9. View,
  10. Image,
  11. TouchableOpacity,
  12. Platform
  13. } from 'react-native';
  14.  
  15. var Home = React.createClass({
  16. render() {
  17. return (
  18. <View style={styles.container}>
  19. {/*导航条*/}
  20. {this.renderNavBar()}
  21. <Text style={styles.welcome}>
  22. 首页
  23. </Text>
  24. </View>
  25. );
  26. },
  27. // 导航条
  28. renderNavBar(){
  29. return(
  30. <View style={styles.navOutViewStyle}>
  31. <Text style={{color:'white',fontSize:16,fontWeight:'bold'}}>首页</Text>
  32. </View>
  33. )
  34. }
  35. });
  36.  
  37. const styles = StyleSheet.create({
  38. // 导航条视图
  39. navOutViewStyle:{
  40. height:Platform.OS === 'ios' ? 64 : 44,
  41. backgroundColor:'#468AFF',
  42. // 主轴方向
  43. flexDirection:'row',
  44. // 侧轴对齐方式 垂直居中
  45. alignItems:'center',
  46. // 主轴方向居中
  47. justifyContent:'center',
  48. },
  49. container: {
  50. flex: 1,
  51. backgroundColor: '#F5FCFF',
  52. },
  53. welcome: {
  54. fontSize: 20,
  55. textAlign: 'center',
  56. margin: 10,
  57. },
  58. });
  59.  
  60. // 输出类
  61. module.exports = Home;

3.Message.js

  1. /**
  2. * 消息
  3. */
  4. import React, { Component } from 'react';
  5. import {
  6. AppRegistry,
  7. StyleSheet,
  8. Text,
  9. View,
  10. Image,
  11. TouchableOpacity,
  12. Platform
  13. } from 'react-native';
  14.  
  15. var Message = React.createClass({
  16. render() {
  17. return (
  18. <View style={styles.container}>
  19. {/*导航条*/}
  20. {this.renderNavBar()}
  21. <Text style={styles.welcome}>
  22. 消息
  23. </Text>
  24. </View>
  25. );
  26. },
  27. // 导航条
  28. renderNavBar(){
  29. return(
  30. <View style={styles.navOutViewStyle}>
  31. <Text style={{color:'white',fontSize:16,fontWeight:'bold'}}>消息</Text>
  32. </View>
  33. )
  34. }
  35. });
  36.  
  37. const styles = StyleSheet.create({
  38. // 导航条视图
  39. navOutViewStyle:{
  40. height:Platform.OS === 'ios' ? 64 : 44,
  41. backgroundColor:'#468AFF',
  42. // 主轴方向
  43. flexDirection:'row',
  44. // 侧轴对齐方式 垂直居中
  45. alignItems:'center',
  46. // 主轴方向居中
  47. justifyContent:'center',
  48. },
  49. container: {
  50. flex: 1,
  51. backgroundColor: '#F5FCFF',
  52. },
  53. welcome: {
  54. fontSize: 20,
  55. textAlign: 'center',
  56. margin: 10,
  57. },
  58. });
  59.  
  60. // 输出类
  61. module.exports = Message;

4.Find.js

  1. /**
  2. * 发现
  3. */
  4. import React, { Component } from 'react';
  5. import {
  6. AppRegistry,
  7. StyleSheet,
  8. Text,
  9. View,
  10. Image,
  11. TouchableOpacity,
  12. Platform
  13. } from 'react-native';
  14.  
  15. var Find = React.createClass({
  16. render() {
  17. return (
  18. <View style={styles.container}>
  19. {/*导航条*/}
  20. {this.renderNavBar()}
  21. <Text style={styles.welcome}>
  22. 发现
  23. </Text>
  24. </View>
  25. );
  26. },
  27. // 导航条
  28. renderNavBar(){
  29. return(
  30. <View style={styles.navOutViewStyle}>
  31. <Text style={{color:'white',fontSize:16,fontWeight:'bold'}}>发现</Text>
  32. </View>
  33. )
  34. }
  35. });
  36.  
  37. const styles = StyleSheet.create({
  38. // 导航条视图
  39. navOutViewStyle:{
  40. height:Platform.OS === 'ios' ? 64 : 44,
  41. backgroundColor:'#468AFF',
  42. // 主轴方向
  43. flexDirection:'row',
  44. // 侧轴对齐方式 垂直居中
  45. alignItems:'center',
  46. // 主轴方向居中
  47. justifyContent:'center',
  48. },
  49. container: {
  50. flex: 1,
  51. backgroundColor: '#F5FCFF',
  52. },
  53. welcome: {
  54. fontSize: 20,
  55. textAlign: 'center',
  56. margin: 10,
  57. },
  58. });
  59.  
  60. // 输出类
  61. module.exports = Find;

5.Mine.js

  1. /**
  2. * 我
  3. */
  4. import React, { Component } from 'react';
  5. import {
  6. AppRegistry,
  7. StyleSheet,
  8. Text,
  9. View,
  10. Image,
  11. TouchableOpacity,
  12. Platform
  13. } from 'react-native';
  14.  
  15. var Mine = React.createClass({
  16. render() {
  17. return (
  18. <View style={styles.container}>
  19. {/*导航条*/}
  20. {this.renderNavBar()}
  21. <Text style={styles.welcome}>
  22. 我的
  23. </Text>
  24. </View>
  25. );
  26. },
  27. // 导航条
  28. renderNavBar(){
  29. return(
  30. <View style={styles.navOutViewStyle}>
  31. <Text style={{color:'white',fontSize:16,fontWeight:'bold'}}>我的</Text>
  32. </View>
  33. )
  34. }
  35. });
  36.  
  37. const styles = StyleSheet.create({
  38. // 导航条视图
  39. navOutViewStyle:{
  40. height:Platform.OS === 'ios' ? 64 : 44,
  41. backgroundColor:'#468AFF',
  42. // 主轴方向
  43. flexDirection:'row',
  44. // 侧轴对齐方式 垂直居中
  45. alignItems:'center',
  46. // 主轴方向居中
  47. justifyContent:'center',
  48. },
  49. container: {
  50. flex: 1,
  51. backgroundColor: '#F5FCFF',
  52. },
  53. welcome: {
  54. fontSize: 20,
  55. textAlign: 'center',
  56. margin: 10,
  57. },
  58. });
  59.  
  60. // 输出类
  61. module.exports = Mine;

6.效果图

React Native商城项目实战04 - 封装TabNavigator.Item的创建的更多相关文章

  1. React Native商城项目实战02 - 主要框架部分(tabBar)

    1.安装插件,cd到项目根目录下执行: $ npm i react-native-tab-navigator --save 2.主框架文件Main.js /** * 主页面 */ import Rea ...

  2. React Native商城项目实战03 - 包装Navigator

    1.在Home目录下新建首页详细页HomeDetail.js /** * 首页详情页 */ import React, { Component } from 'react'; import { App ...

  3. React Native商城项目实战01 - 初始化设置

    1.创建项目 $ react-native init BuyDemo 2.导入图片资源 安卓:把文件夹放到/android/app/src/main/res/目录下,如图: iOS: Xcode打开工 ...

  4. React Native商城项目实战11 - 个人中心头部内容

    1.创建MineHeaderView.js /** * 个人中心头部内容 */ import React, { Component } from 'react'; import { AppRegist ...

  5. React Native商城项目实战10 - 个人中心中间内容设置

    1.新建一个MineMiddleView.js,专门用于构建中间的内容 /** * 个人中心中间内容设置 */ import React, { Component } from 'react'; im ...

  6. React Native商城项目实战07 - 设置“More”界面导航条

    1.More/More.js /** * 更多 */ import React, { Component } from 'react'; import { AppRegistry, StyleShee ...

  7. React Native商城项目实战05 - 设置首页的导航条

    1.Home.js /** * 首页 */ import React, { Component } from 'react'; import { AppRegistry, StyleSheet, Te ...

  8. React Native商城项目实战06 - 设置安卓中的启动页

    1.Main 目录下新建LaunchImage.js: /** * 启动页 */ import React, { Component } from 'react'; import { AppRegis ...

  9. React Native商城项目实战16 - 购物中心详细页

    逻辑分析: 首页(Home)加载的购物中心组件(ShopCenter),传递url数据: ShopCenter里根据url加载购物中心详细页组件(ShopCenterDetail), ShopCent ...

随机推荐

  1. Python 经典面试题(一)

    一.浮点数运算 题目 判断浮点数的运行结果是否相等:  a = 0.1 b = 0.2 c = 0.3 assert a + b == c 题目解析: 本题考查的是计算机的浮点运算知识点.不仅是 py ...

  2. 运维LVS-NAT模式理解

    一.LVS-NAT模式的工作原理这个是通过网络地址转换的方法来实现调度的.首先调度器(LB)接收到客户的请求数据包时(请求的目的IP为VIP),根据调度算法决定将请求发送给哪个 后端的真实服务器(RS ...

  3. css3 加载动画

    代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8& ...

  4. sqlite3中 timestamp使用

    timestamp使用 一. timestamp两种属性:自动初始化: 此行为只在第一次写入数据时,怎么把时间设为当前时间. (DEFAULT CURRENT_TIMESTAMP)自动更新: 此行为在 ...

  5. xcode中进行git代码管理

    http://www.cocoachina.com/ios/20140524/8536.html

  6. 【leetcode 476】. Number Complement

    给定一个正整数,输出其补码. 思路:利用mask掩码进行异或, 利用 temp >> 1 大于0 来决定mask长度,求出的mask 为二进制 1 0 0 0 0类型,           ...

  7. Spring Boot自定义Redis缓存配置,保存value格式JSON字符串

    Spring Boot自定义Redis缓存,保存格式JSON字符串 部分内容转自 https://blog.csdn.net/caojidasabi/article/details/83059642 ...

  8. web框架-(七)Django补充---models进阶操作及modelform操作

    通过之前的课程我们可以对于Django的models进行简单的操作,今天了解下进阶操作和modelform: 1. Models进阶操作 1.1 字段操作 AutoField(Field) - int ...

  9. Elasticsearch:hanlp 中文分词器

    HanLP 中文分词器是一个开源的分词器,是专为Elasticsearch而设计的.它是基于HanLP,并提供了HanLP中大部分的分词方式.它的源码位于: https://github.com/Ke ...

  10. rediscli命令

    一.rediscli xxx 发送命令 二.进入客户端后的命令