React-native 底部导航栏(二)
1、组件安装:npm install react-native-router-flux --save
2、定义菜单图片和文字:
import React, { Component } from 'react';
import { View, Image, Text, StyleSheet,Dimensions } from 'react-native';
class TabIcon extends Component {
constructor(props){
super(props);
}
render(){
let selected=this.props.focused;
let data={
home:{
title:"首页",
icon:!selected?require("../resource/images/home.png"):require("../resource/images/home_selected.png")
},
movies:{
title:"电影",
icon:!selected?require("../resource/images/movies.png"):require("../resource/images/movies_selected.png")
},
theaters:{
title:"影院",
icon:!selected?require("../resource/images/theater.png"):require("../resource/images/theater_selected.png")
},
me:{
title:"我",
icon:!selected?require("../resource/images/me.png"):require("../resource/images/me_selected.png")
}
}
let param=data[this.props.navigation.state.key];
return <View style={styles.tabbarContainer}>
<Image style={{ width: 25, height: 25,resizeMode:'contain' }} source={param.icon} />
<Text style={[styles.tabbarItem,selected&&{color:'#F08519'}]}>{param.title}</Text>
</View>
}
}
const styles = StyleSheet.create({
tabbarContainer:{
flex:1,
alignItems:"center",
justifyContent:"center",
width:Dimensions.get('window').width/4
},
tabbarItem:{
marginTop:5,
textAlign:"center"
}
});
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、定义底部导航栏:
import React, { Component } from 'react';
import { Provider } from 'react-redux';
import PropTypes from 'prop-types';
import {
View,
Text,
BackAndroid,
StyleSheet
} from 'react-native';
import { Scene, Router, TabBar, Modal, Schema, Actions, Reducer, ActionConst } from 'react-native-router-flux';
import { connect } from 'react-redux';
import LoginPage from './modules/auth/containers/loginPage';
import HomeIndex from './modules/home/containers/indexPage';
import TabIcon from './common/tabIcon';
class AppRoot extends Component {
static propTypes = {
dispatch: PropTypes.func
}
constructor(props) {
super(props);
}
createReducer(params) {
const defaultReducer = Reducer(params);
return (state, action) => {
this.props.dispatch(action);
return defaultReducer(state, action);
};
}
onExitApp(){
BackAndroid.exitApp();
return true;
}
render() {
return (
<Router onExitApp={this.onExitApp}
createReducer={ this.createReducer.bind(this) }
scenes={ scenes }
>
</Router >
)
}
}
const styles = StyleSheet.create({
tabBarStyle: {
backgroundColor: '#fff',
height:64
},
tabBarSelectedItemStyle: {
backgroundColor: '#fff'
},
titleStyle: {
color: '#fff'
},
})
const scenes = Actions.create(
<Scene key="root" hideNavBar={true}>
<Scene key="login" component={LoginPage} title="登录" hideNavBar={true} />
<Scene key="tabbar"
initial
tabs={true}
tabBarPosition="bottom"
showLabel={false}
tabBarStyle={styles.tabBarStyle}
tabBarSelectedItemStyle={styles.tabBarSelectedItemStyle}
titleStyle={styles.titleStyle}>
<Scene key="home"
hideNavBar={true}
component={HomeIndex}
icon={TabIcon}
titleStyle={styles.titleStyle}/>
<Scene key="movies"
hideNavBar={true}
component={HomeIndex}
icon={TabIcon}
titleStyle={styles.titleStyle} />
<Scene key="theaters"
hideNavBar={true}
component={HomeIndex}
icon={TabIcon}
titleStyle={styles.titleStyle} />
<Scene key="me"
hideNavBar={true}
component={LoginPage}
icon={TabIcon}
titleStyle={styles.titleStyle} />
</Scene>
</Scene>
)
export default connect()(AppRoot);
由于此示例基于redux,为完整项目结构,还需做以下处理:
- 定义dispatch
import PropTypes from 'prop-types';
...
class AppRoot extends Component {
static propTypes = {
dispatch: PropTypes.func
}
...
}
- 使用connect连接React组件
export default connect()(AppRoot);
GIT源码地址:react-native-demo 分支名称:tabbar
请查看原文:https://www.jianshu.com/p/ab7eb90034fd
React-native 底部导航栏(二)的更多相关文章
- React Native 底部导航栏
首先安装:npm install react-native-tab-navigator 然后再引入文件中 import TabNavigator from 'react-native-tab ...
- [RN] React Native 自定义导航栏随滚动渐变
React Native 自定义导航栏随滚动渐变 实现效果预览: 代码实现: 1.定义导航栏 NavPage.js import React, {Component} from 'react'; im ...
- React Native自定义导航栏
之前我们学习了可触摸组件和页面导航的使用的使用: 从零学React Native之09可触摸组件 - 从零学React Native之03页面导航 - 经过之前的学习, 我们可以完成一个自定义导航栏了 ...
- react native底部tab栏切换
1.安装tab栏插件 npm i react-native-tab-navigator --save 2.引入对应的组件和tab插件 import { Platform, StyleSheet, Te ...
- React Native 之导航栏
一定要参考官网: https://reactnavigation.org/docs/en/getting-started.html 代码来自慕课网:https://www.imooc.com/cour ...
- React Native(四)——顶部以及底部导航栏实现方式
效果图: 一步一步慢慢来: 其实刚入手做app的时候,就应该做出简单的顶部以及底部导航栏.无奈又在忙其他事情,导致这些现在才整理出来. 1.顶部导航栏:react-native-scrollable- ...
- 二、Fragment+RadioButton实现底部导航栏
在App中经常看到这样的tab底部导航栏 那么这种效果是如何实现,实现的方式有很多种,最常见的就是使用Fragment+RadioButton去实现.下面我们来写一个例子 首先我们先在activi ...
- TextView+Fragment实现底部导航栏
前言:项目第二版刚上线没多久,产品又对需求进行了大改动,以前用的是左滑菜单,现在又要换成底部导航栏,于是今天又苦逼加班了.花了几个小时实现了一个底部导航栏的demo,然后总结一下.写一篇博客.供自己以 ...
- Android 修改底部导航栏navigationbar的颜色
Android 修改底部导航栏navigationbar的颜色 getWindow().setNavigationBarColor(Color.BLUE); //写法一 getWindow().set ...
随机推荐
- Spring Boot使用阿里云证书启用HTTPS
1.到阿里云下载证书页面下载证书 2.根据页面内容,可以使用2种证书:PFX JKS 把对应证书放到src/main/resources目录下 在application.properties文件中加入 ...
- jsp里面include的静态导入和动态导入的区别
静态导入就是将被导入页面完全融入到导入的页面中:而动态导入只是在servlet里面插入了include方法,导入的这是被导入页面的body标签里面的内容 1.什么是静态导入? 静态导入指的是,将一个外 ...
- maven国内镜像、国内外仓库(直接下载jar)
阿里: https://maven.aliyun.com/mvn/search 官方: http://repo.maven.apache.org/maven2/ maven仓库 阿里巴巴的镜像仓库, ...
- nodejs 配置服务器
node 是 js 的运行的后台环境,他自身集成了很多模块,集成的模块直接 require 就行了: npm 第三方平台,他也是为 node 服务的,对于 npm 中的模块,先 npm install ...
- ubuntu下mysql数据库存储路径修改
一.安装mysql ubuntu系统安装配置APT源,apt install mysql-server mysql-client 二.查看安装端口情况 sudo netstat -tap | grep ...
- jquery 登录判断遇到的小问题
1.碰到的第一个问题是: 往body上加载check,用load不管用,可以用ready试试. 2.原来jquery里获取用的val(),我一直以为是value()... 尴尬 3.两个标志位是为了判 ...
- IDEA常用智能提示
psvm: 生成代码: public static void main(String[] args) { }
- 四十一:数据库之SQLAlchemy之limlt、、slice、offset及切片
一:limit:限制每次查询的时候查询数据的条数二:slice:查一个区间的数据,slice(起,止)三:offset:限制查找数据的时候过滤掉前面多少条四:切片:对query对象切实获取想要的数据 ...
- 【HANA系列】SAP HANA计算视图中的RANK使用方法
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA计算视图中的RA ...
- LeetCode.12-整数转罗马数字符串(Integer to Roman)
这是悦乐书的第351次更新,第376篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第6题(顺位题号是12).罗马数字由七个不同的符号表示:I,V,X,L,C,D和M. ...