[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 ...
随机推荐
- stone2 [期望]
也许更好的阅读体验 \(\mathcal{Description}\) 有 \(n\) 堆石子,依次编号为 \(1, 2,\ldots , n\),其中第 \(i\) 堆有 \(a_i\) 颗石子 你 ...
- java之spring mvc之初始spring mvc
1. mvc : mvc框架是处理 http请求和响应的框架 2. mvc 做的事情有哪些: 将 url 映射到一个java的处理方法上 将表单数据提交到 java 类中 将后台 java 类处理的结 ...
- [個人紀錄] WindowsLiveWriter 插入代碼跳出錯誤
跳出找不到設定檔Can’t load configruaration fromC:\Users\…\AppData\Roaming\Windows Live Writer\WindowsLiveWri ...
- mybatis中参数为list集合时使用 mybatis in查询
mybatis中参数为list集合时使用 mybatis in查询 一.问题描述mybatis sql查询时,若遇到多个条件匹配一个字段,sql 如: select * from user where ...
- 利用nfs-client-provisioner动态提供Kubernetes后端存储卷
原文:https://www.kubernetes.org.cn/3894.html 利用NFS client provisioner动态提供Kubernetes后端存储卷 本文翻译自nfs-clie ...
- 换个语言学一下 Golang (7)——使用函数
什么是函数 函数,简单来讲就是一段将输入数据转换为输出数据的公用代码块.当然有的时候函数的返回值为空,那么就是说输出数据为空.而真正的处理过程在函数内部已经完成了. 想一想我们为什么需要函数,最直接的 ...
- crunch制作字典
安装 安装crunch sudo apt-get install crunch 语法 crunch <min> max<max> <characterset> -t ...
- 2.在HTML中使用JavaScript
目录 1. script元素 2. 标签的位置 3.延迟和异步加载 4.嵌入代码与外部代码的区别 5.noscript元素 6. 小结 1. script元素 向HTML中插入JavaScript的主 ...
- index.jsp乱码问题的解决
我们在做java项目的时候,都会有个首页,一般就是index.jsp,然后在index.jsp中引入相关的文件,一般也是引入打包过后的相关资源文件. 当index.jsp上面的中文出现乱码的时候,就需 ...
- Jenkins+Docker+Git+Harbor流水线打包
Jenkins+Docker+Git+Harbor流水线打包 环境: CentOS Linux release 7.6.1810 (Core) 192.168.247.214 Jenkins+dock ...