[RN] React Native 下实现底部标签(不支持滑动切换)
底部标签是现在App的基本菜单实现
下面分别用 createBottomTabNavigator 和 createMaterialBottomTabNavigator 两种方法分别实现底部菜单
但此两种方法实现的效果,均 不支持滑动切换 ,支持滑动切换 会在后续文章中增加
效果预览如下:

先做一些准备工作:
1、index.js
/**
* @format
*/ import {AppRegistry} from 'react-native';
import App from './src/App';
import {name as appName} from './app.json'; AppRegistry.registerComponent(appName, () => App);
2、四个菜单文件,以src/Home.js为例
import React, {Component} from 'react';
import {
View,
Text,
StyleSheet,
TouchableOpacity,
} from 'react-native';
class Home extends Component {
render() {
return (
<View style={styles.container}>
<TouchableOpacity style={styles.button} activeOpacity={0.5}>
<Text style={{color: 'white'}}>首页</Text>
</TouchableOpacity>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: ,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
button: {
width: ,
height: ,
borderRadius: ,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: '#4398ff',
}
});
module.exports = Home;
其他几个菜单,也知识 显示的文字有差别而已!
第一种方法,使用 createBottomTabNavigator :
1)安装依赖
npm install react-navigation --save
安装完后项目根目录下Package.json文件中依赖如下:
"react": "16.8.3",
"react-native": "0.59.5",
"react-native-gesture-handler": "^1.2.1",
"react-navigation": "^3.9.1"
2)调用代码src/App.js
import React, {Component} from 'react';
import {Image} from 'react-native';
import {
createBottomTabNavigator, createAppContainer
} from 'react-navigation';
//展示的页面
import Home from './Home';
import Contact from './Contact';
import Discover from './Discover';
import Mine from './Mine';
//Tab
const TabNavigator = createBottomTabNavigator({
Home: {
screen: Home,//当前选项卡加载的页面
navigationOptions: {
tabBarLabel: '新闻',
tabBarIcon: ({tintColor, focused}) => (
<Image
source={focused ? require('./main_tab_home_icon_pressed.png') : require('./main_tab_home_icon.png')}
style={[{height: , width: }]}
/>
),
},
},
Contact: {
screen: Contact,
navigationOptions: {
tabBarLabel: '视频',
tabBarIcon: ({tintColor, focused}) => (
<Image
source={focused ? require('./main_tab_video_icon_pressed.png') : require('./main_tab_video_icon.png')}
style={[{height: , width: }]}
/>
),
},
},
Discover: {
screen: Discover,
navigationOptions: {
tabBarLabel: '图片',
tabBarIcon: ({tintColor, focused}) => (
<Image
source={focused ? require('./main_tab_image_icon_pressed.png') : require('./main_tab_image_icon.png')}
style={[{height: , width: }]}/>
),
}
},
Mine: {
screen: Mine,
navigationOptions: {
tabBarLabel: '我的',
tabBarIcon: ({tintColor, focused}) => (
<Image
source={focused ? require('./main_tab_user_icon_pressed.png') : require('./main_tab_user_icon.png')}
style={[{height: , width: }]}/>
),
}
},
}, {
tabBarOptions: {
activeTintColor: '#45C018',
}
});
export default createAppContainer(TabNavigator);
第二种方法,使用 createMaterialBottomTabNavigator:
1)安装依赖 (在前面 npm install react-navigation --save 的基础上再安装)
npm install react-navigation-material-bottom-tabs react-native-paper react-native-vector-icons --save
安装完后项目根目录下Package.json文件中依赖如下:
"react": "16.8.3",
"react-native": "0.59.5",
"react-native-gesture-handler": "^1.2.1",
"react-native-paper": "^2.15.2",
"react-native-vector-icons": "^6.4.2",
"react-navigation": "^3.9.1",
"react-navigation-material-bottom-tabs": "^1.0.0"
2)调用代码src/App.js
import React from 'react';
import {Image} from 'react-native';
import {createAppContainer} from 'react-navigation';
import {createMaterialBottomTabNavigator} from "react-navigation-material-bottom-tabs"; //展示的页面
import Home from './Home';
import Contact from './Contact';
import Discover from './Discover';
import Mine from './Mine'; //Tab
const TabNavigator = createMaterialBottomTabNavigator({
Home: {
screen: Home,//当前选项卡加载的页面
navigationOptions: {
tabBarLabel: '新闻',
tabBarIcon: ({tintColor, focused}) => (
<Image
source={focused ? require('./main_tab_home_icon_pressed.png') : require('./main_tab_home_icon.png')}
style={[{height: , width: }]}
/>
),
},
},
Contact: {
screen: Contact,
navigationOptions: {
tabBarLabel: '视频',
tabBarIcon: ({tintColor, focused}) => (
<Image
source={focused ? require('./main_tab_video_icon_pressed.png') : require('./main_tab_video_icon.png')}
style={[{height: , width: }]}
/>
),
},
},
Discover: {
screen: Discover,
navigationOptions: {
tabBarLabel: '图片',
tabBarIcon: ({tintColor, focused}) => (
<Image
source={focused ? require('./main_tab_image_icon_pressed.png') : require('./main_tab_image_icon.png')}
style={[{height: , width: }]}/>
),
}
},
Mine: {
screen: Mine,
navigationOptions: {
tabBarLabel: '我的',
tabBarIcon: ({tintColor, focused}) => (
<Image
source={focused ? require('./main_tab_user_icon_pressed.png') : require('./main_tab_user_icon.png')}
style={[{height: , width: }]}/>
),
}
},
}, {
activeColor: '#45C018',
inactiveColor: '#111111',
shifting: false,
barStyle: {
backgroundColor: '#fff',
}
}); export default createAppContainer(TabNavigator);
本博客地址: wukong1688
本文原文地址:https://www.cnblogs.com/wukong1688/p/10819708.html
转载请注明出处!谢谢~~
[RN] React Native 下实现底部标签(不支持滑动切换)的更多相关文章
- [RN] React Native 下实现底部标签(支持滑动切换)
上一篇文章 [RN] React Native 下实现底部标签(不支持滑动切换) 总结了不支持滑动切换的方法,此篇文章总结出 支持滑动 的方法 准备工作之类的,跟上文类似,大家可点击上文查看相关内容. ...
- [RN] React Native 下拉放大动画
React Native 下拉放大动画 经测试,无法运行 https://www.jianshu.com/p/1c960ad75020
- [RN] React Native 实现 多选标签
React Native 实现 多选标签 效果如下: 实现代码: import React, {Component} from 'react'; import {Button, StyleSheet, ...
- [RN] React Native 下列表 FlatList 和 SectionList
1.FlatList FlatList组件用于显示一个垂直的滚动列表,其中的元素之间结构近似而仅数据不同. FlatList更适于长列表数据,且元素个数可以增删.和ScrollView不同的是,Fla ...
- [RN] React Native 幻灯片效果 Banner
[RN] React Native 幻灯片效果 Banner 1.定义Banner import React, {Component} from 'react'; import {Image, Scr ...
- [RN] React Native 实现 类似QQ 登陆页面
[RN] React Native 实现 类似QQ 登陆页面 一.主页index.js 项目目录下index.js /** * @format */ import {AppRegistry} from ...
- [RN] React Native 常见基本问题归纳总结
[RN] React Native 常见基本问题归纳总结 本问题总结涉及到版本为: "react": "16.8.3","react-native& ...
- [RN] React Native 实现图片预览
[RN] React Native 实现图片预览 效果预览: 代码如下: 'use strict'; import React, {Component} from 'react'; import {I ...
- [RN] React Native 关闭所有黄色警告
[RN] React Native 关闭所有黄色警告 console.ignoredYellowBox = ['Warning: BackAndroid is deprecated. Please u ...
随机推荐
- FastJson前置属性过滤器
FastJson前置属性过滤器 /** * <html> * <body> * <P> Copyright 1994 JsonInternational</p ...
- ABP(ASP.NET Boilerplate Project)学习总结
ABP(ASP.NET Boilerplate Project),现下比较流行的一种web框架,因为公司新项目准备使用这种框架,所以写下这篇文章记录下自己一步一步搭建的过程,就当做是对学习的一个总结与 ...
- 十大Intellij IDEA快捷键(附IDEA快捷键详细列表及使用技巧)
十大Intellij IDEA快捷键(附IDEA快捷键详细列表及使用技巧) Intellij IDEA中有很多快捷键让人爱不释手,stackoverflow上也有一些有趣的讨论.每个人都有自己的最爱, ...
- eyoucms 模板
https://www.oschina.net/p/eyoucms 下载模板 https://www.eyoucms.com/doc/operation/ 学习手册
- PropTypes.element和PropTypes.node的区别
PropTypes.element:指React Element,即React.CreateElement生成的元素,React.CreateElement可以用jsx语法糖表示: <MyBut ...
- 【RAC】 RAC For W2K8R2 安装--创建ASM磁盘组(六)
[RAC] RAC For W2K8R2 安装--创建ASM磁盘组(六) 一.1 BLOG文档结构图 一.2 前言部分 一.2.1 导读 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以 ...
- Python学习日记(二十七) 反射和几个内置函数
isinstance() 判断isinstance(obj,cls)中obj是否是cls类的对象 class Person: def __init__(self,name): self.name = ...
- 8.7 —— 排序函数及 splice 插入
.排序,按自己的逻辑 nid_item_vec.sort([](const NID_PBDATA &l, const NID_PBDATA &r) -> bool { retur ...
- oracle 字符串分隔去重函数
create or replaceFUNCTION "SF_SPLIT_ACCOUNT_ID_LIST" ( account_id_list IN VARCHAR2)RETURN ...
- SQL进阶系列之9用SQL处理数列
写在前面 关系模型的数据结构里,并没有顺序的概念,但SQL处理有序集合也有坚实的理论基础 生成连续编号 --生成连续编号 CREATE TABLE Digits (digit INTEGER PRIM ...