1、组件安装:npm install react-native-router-flux --save

2、定义菜单图片和文字:

  

  1. import React, { Component } from 'react';
  2. import { View, Image, Text, StyleSheet,Dimensions } from 'react-native';
  3. class TabIcon extends Component {
  4. constructor(props){
  5. super(props);
  6. }
  7.  
  8. render(){
  9. let selected=this.props.focused;
  10. let data={
  11. home:{
  12. title:"首页",
  13. icon:!selected?require("../resource/images/home.png"):require("../resource/images/home_selected.png")
  14. },
  15. movies:{
  16. title:"电影",
  17. icon:!selected?require("../resource/images/movies.png"):require("../resource/images/movies_selected.png")
  18. },
  19. theaters:{
  20. title:"影院",
  21. icon:!selected?require("../resource/images/theater.png"):require("../resource/images/theater_selected.png")
  22. },
  23. me:{
  24. title:"我",
  25. icon:!selected?require("../resource/images/me.png"):require("../resource/images/me_selected.png")
  26. }
  27. }
  28. let param=data[this.props.navigation.state.key];
  29. return <View style={styles.tabbarContainer}>
  30. <Image style={{ width: 25, height: 25,resizeMode:'contain' }} source={param.icon} />
  31. <Text style={[styles.tabbarItem,selected&&{color:'#F08519'}]}>{param.title}</Text>
  32. </View>
  33. }
  34. }
  35.  
  36. const styles = StyleSheet.create({
  37. tabbarContainer:{
  38. flex:1,
  39. alignItems:"center",
  40. justifyContent:"center",
  41. width:Dimensions.get('window').width/4
  42. },
  43. tabbarItem:{
  44. marginTop:5,
  45. textAlign:"center"
  46. }
  47. });
  48.  
  49. module.exports = TabIcon;
  • 判断菜单是否被选中this.props.focused在老版本的react-native-router-flux使用this.props.selected;
  • 取当前菜单this.props.navigation.state.key在老版本的react-native-router-flux使用this.props.sceneKey

3、定义底部导航栏:

  1. import React, { Component } from 'react';
  2. import { Provider } from 'react-redux';
  3. import PropTypes from 'prop-types';
  4. import {
  5. View,
  6. Text,
  7. BackAndroid,
  8. StyleSheet
  9. } from 'react-native';
  10. import { Scene, Router, TabBar, Modal, Schema, Actions, Reducer, ActionConst } from 'react-native-router-flux';
  11. import { connect } from 'react-redux';
  12. import LoginPage from './modules/auth/containers/loginPage';
  13. import HomeIndex from './modules/home/containers/indexPage';
  14. import TabIcon from './common/tabIcon';
  15.  
  16. class AppRoot extends Component {
  17. static propTypes = {
  18. dispatch: PropTypes.func
  19. }
  20.  
  21. constructor(props) {
  22. super(props);
  23. }
  24.  
  25. createReducer(params) {
  26. const defaultReducer = Reducer(params);
  27. return (state, action) => {
  28. this.props.dispatch(action);
  29. return defaultReducer(state, action);
  30. };
  31. }
  32.  
  33. onExitApp(){
  34. BackAndroid.exitApp();
  35. return true;
  36. }
  37. render() {
  38. return (
  39. <Router onExitApp={this.onExitApp}
  40. createReducer={ this.createReducer.bind(this) }
  41. scenes={ scenes }
  42. >
  43. </Router >
  44. )
  45. }
  46. }
  47.  
  48. const styles = StyleSheet.create({
  49. tabBarStyle: {
  50. backgroundColor: '#fff',
  51. height:64
  52. },
  53. tabBarSelectedItemStyle: {
  54. backgroundColor: '#fff'
  55. },
  56. titleStyle: {
  57. color: '#fff'
  58. },
  59. })
  60.  
  61. const scenes = Actions.create(
  62. <Scene key="root" hideNavBar={true}>
  63. <Scene key="login" component={LoginPage} title="登录" hideNavBar={true} />
  64. <Scene key="tabbar"
  65. initial
  66. tabs={true}
  67. tabBarPosition="bottom"
  68. showLabel={false}
  69. tabBarStyle={styles.tabBarStyle}
  70. tabBarSelectedItemStyle={styles.tabBarSelectedItemStyle}
  71. titleStyle={styles.titleStyle}>
  72. <Scene key="home"
  73. hideNavBar={true}
  74. component={HomeIndex}
  75. icon={TabIcon}
  76. titleStyle={styles.titleStyle}/>
  77.  
  78. <Scene key="movies"
  79. hideNavBar={true}
  80. component={HomeIndex}
  81. icon={TabIcon}
  82. titleStyle={styles.titleStyle} />
  83.  
  84. <Scene key="theaters"
  85. hideNavBar={true}
  86. component={HomeIndex}
  87. icon={TabIcon}
  88. titleStyle={styles.titleStyle} />
  89.  
  90. <Scene key="me"
  91. hideNavBar={true}
  92. component={LoginPage}
  93. icon={TabIcon}
  94. titleStyle={styles.titleStyle} />
  95. </Scene>
  96. </Scene>
  97. )
  98. export default connect()(AppRoot);

由于此示例基于redux,为完整项目结构,还需做以下处理:

  • 定义dispatch
  1. import PropTypes from 'prop-types';
  2. ...
  3. class AppRoot extends Component {
  4. static propTypes = {
  5. dispatch: PropTypes.func
  6. }
  7. ...
  8. }
  • 使用connect连接React组件

export default connect()(AppRoot);

  GIT源码地址:react-native-demo 分支名称:tabbar

请查看原文:https://www.jianshu.com/p/ab7eb90034fd

React-native 底部导航栏(二)的更多相关文章

  1. React Native 底部导航栏

    首先安装:npm install react-native-tab-navigator   然后再引入文件中    import TabNavigator from 'react-native-tab ...

  2. [RN] React Native 自定义导航栏随滚动渐变

    React Native 自定义导航栏随滚动渐变 实现效果预览: 代码实现: 1.定义导航栏 NavPage.js import React, {Component} from 'react'; im ...

  3. React Native自定义导航栏

    之前我们学习了可触摸组件和页面导航的使用的使用: 从零学React Native之09可触摸组件 - 从零学React Native之03页面导航 - 经过之前的学习, 我们可以完成一个自定义导航栏了 ...

  4. react native底部tab栏切换

    1.安装tab栏插件 npm i react-native-tab-navigator --save 2.引入对应的组件和tab插件 import { Platform, StyleSheet, Te ...

  5. React Native 之导航栏

    一定要参考官网: https://reactnavigation.org/docs/en/getting-started.html 代码来自慕课网:https://www.imooc.com/cour ...

  6. React Native(四)——顶部以及底部导航栏实现方式

    效果图: 一步一步慢慢来: 其实刚入手做app的时候,就应该做出简单的顶部以及底部导航栏.无奈又在忙其他事情,导致这些现在才整理出来. 1.顶部导航栏:react-native-scrollable- ...

  7. 二、Fragment+RadioButton实现底部导航栏

    在App中经常看到这样的tab底部导航栏   那么这种效果是如何实现,实现的方式有很多种,最常见的就是使用Fragment+RadioButton去实现.下面我们来写一个例子 首先我们先在activi ...

  8. TextView+Fragment实现底部导航栏

    前言:项目第二版刚上线没多久,产品又对需求进行了大改动,以前用的是左滑菜单,现在又要换成底部导航栏,于是今天又苦逼加班了.花了几个小时实现了一个底部导航栏的demo,然后总结一下.写一篇博客.供自己以 ...

  9. Android 修改底部导航栏navigationbar的颜色

    Android 修改底部导航栏navigationbar的颜色 getWindow().setNavigationBarColor(Color.BLUE); //写法一 getWindow().set ...

随机推荐

  1. CentOS 6.5上的Tomcat启动报错问题

    最近在搭建虚拟机环境,装的是CentOSQL 6.5版本,然后装的OpenJDK1.7,在Apache下载了一个纯净的Tomcat放到虚拟机上启动报错了: 这里有两个错误: 1.第一个错误,APR的问 ...

  2. 第十四周课程总结 & 实验报告

    一.JDBC JDBC概述 JDBC提供了一种与平台无关的用于执行SQL语句的标准JavaAPI,可以方便的实现多种关系型数据库的统一操作,它由一组用Java语言编写的类和接口组成 JDBC的主要操作 ...

  3. 32位下操作mongodb心得

    本文出处:http://blog.csdn.net/chaijunkun/article/details/7236911,转载请注明. 随着互联网的变革,互联网的内容生成方式也逐渐地从网站生成转为用户 ...

  4. java的引用传递

    public class T{ static class Node{ int value; Node left = null; Node right = null; public Node(int v ...

  5. java下载文件时文件名出现乱码的解决办法

    转: java下载文件时文件名出现乱码的解决办法 2018年01月12日 15:43:32 橙子橙 阅读数:6249   java下载文件时文件名出现乱码的解决办法: String userAgent ...

  6. 阶段3 2.Spring_10.Spring中事务控制_5 spring事务控制的代码准备

    创建一个工程,只搭建环境不做配置.等配置的时候把这个项目相关的代码再复制到新项目里面 jar包的打包方式 导入包 事务控制也是基于AOP的.所以这里导入aspectjweaver 复制jdbcTemp ...

  7. 三十六:数据库之SQLAlchemy外建之一对一关系

    relationship()的uselist参数默认为True,即一对多,如果要一对一,则需让uselist=False 准备工作 from sqlalchemy import create_engi ...

  8. 收货确定 BAPI BAPI_GOODSMVT_CREATE

    CLEAR gmhead.     gmhead-pstng_date = ls_table-gzdate."sy-datum .     gmhead-doc_date = sy-datu ...

  9. ping一个网段下的所有ip

    for /l %i in (1,1,255) do ping -n 1 -w 60 192.168.0.%i | find "Reply" >>d:\pingall.l ...

  10. HCL试验2

    PC端配置:配置ip地址 交换机1配置:①创建VLAN system-view vlan 10 vlan 20 ②配置PC端接口 interface gi 1/0/1 port link-type a ...