React Native顶|底部导航使用小技巧

导航一直是App开发中比较重要的一个组件,ReactNative提供了两种导航组件供我们使用,分别是:NavigatorIOS和Navigator,但是前者只能用于iOS平台,后者在ReactNative0.44版本以后已经被移除了。

好在有人提供了更好的导航组件,就是我们今天要讲的react-navigation,并且ReactNative官方更推荐我们使用此组件。

 

本篇文章只讲解基础用法,如果你想了解更多,请戳这里->戳我。

 简介

react-navigation主要包括导航,底部tab,顶部tab,侧滑等,分别为:

  • 导航 -> StackNavigator
  • 底部或者顶部tab -> TabNavigator
  • 侧滑 -> DrawerNavigator

我们今天主要讲TabNavigator。

 效果展示

 实现代码

import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Button,
Text,
View,
Image,
StatusBar
} from 'react-native';
import { StackNavigator, TabBarBottom, TabNavigator } from "react-navigation";
class Home extends React.Component {
static navigationOptions = {
tabBarLabel: '热点',
tabBarIcon: ({ focused, tintColor }) => (
<Image
source={focused ? require('../res/images/hot_hover.png') : require('../res/images/hot.png')}
style={{ width: 26, height: 26, tintColor: tintColor }}
/>
        )
};
render() {
return (
<View style={styles.container}>
<Text>!这是热点</Text>
</View>
        );
}
}
class Circle extends React.Component {
static navigationOptions = {
tabBarLabel: '圈子',
tabBarIcon: ({ focused, tintColor }) => (
<Image
source={focused ? require('../res/images/coterie.png') : require('../res/images/coterie.png')}
style={{ width: 26, height: 26, tintColor: tintColor }}
/>
        )
};
render() {
return (
<View style={styles.container}>
<Text>!这是圈子</Text>
</View>
        );
}
}
class Tools extends React.Component {
static navigationOptions = {
tabBarLabel: '工具',
tabBarIcon: ({ focused, tintColor }) => (
<Image
source={focused ? require('../res/images/tool.png') : require('../res/images/tool.png')}
style={{ width: 26, height: 26, tintColor: tintColor }}
/>
        )
};
render() {
return (
<View style={styles.container}>
<Text>!这是工具</Text>
</View>
        );
}
}
class Mypage extends React.Component {
static navigationOptions = {
tabBarLabel: '我的',
tabBarIcon: ({ focused, tintColor }) => (
<Image
source={focused ? require('../res/images/my_hover.png') : require('../res/images/my.png')}
style={{ width: 26, height: 26, tintColor: tintColor }}
/>
        )
};
render() {
return (
<View style={styles.container}>
<Text>!这是我的</Text>
</View>
        );
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#fff',
}
});
const MyApp = TabNavigator(
{
Home: {
screen: Home,
},
Circle: {
screen: Circle,
},
Tools: {
screen: Tools,
},
Mypage: {
screen: Mypage,
},
},
{
tabBarOptions: {
activeTintColor: '#4BC1D2',
inactiveTintColor: '#000',
showIcon: true,
showLabel: true,
upperCaseLabel: false,
pressColor: '#823453',
pressOpacity: 0.8,
style: {
backgroundColor: '#fff',
paddingBottom: 0,
borderTopWidth: 0.5,
borderTopColor: '#ccc',
},
labelStyle: {
fontSize: 12,
margin: 1
},
indicatorStyle: { height: 0 }, //android 中TabBar下面会显示一条线,高度设为 0 后就不显示线了
        },
tabBarPosition: 'bottom',
swipeEnabled: false,
animationEnabled: false,
lazy: true,
backBehavior: 'none',
});
module.exports = MyApp;

 配置说明

NavigationOptions
当然,通过NavigationOptions来配置我们的tabBarItem:
title - 标题
tabBarVisible - 是否可见
tabBarIcon - 配置图片,当然,完全可以不使用图片
tabBarLabel - 也是配置标题,只不过title既能配置tab的标题,也能配置navigation的标题

 

TabNavigatorConfig
tabBarComponent- 用作标签栏的组件,例如 (这是iOS上的默认设置), (这是Android上的默认设置)TabBarBottomTabBarTop
tabBarPosition- 标签栏的位置可以是或'top''bottom'
swipeEnabled - 是否允许在标签之间进行滑动
animationEnabled - 是否在更改标签时动画
lazy - 是否根据需要懒惰呈现标签,而不是提前制作
tabBarOptions - 配置标签栏,如下所示。
几个选项被传递到底层路由器来修改导航逻辑:
initialRouteName - 首次加载时初始标签路由的routeName
order - 定义选项卡顺序的routeNames数组
paths - 将routeName映射到路径配置,该配置将覆盖routeConfigs中设置的路径。
backBehavior - 后退按钮是否会使Tab键切换到初始选项卡?如果是,否则设置。默认为行为。initialRoutenoneinitialRoute


tabBarOptions for (iOS上的默认标签栏)TabBarBottom
activeTintColor - 活动标签的标签和图标颜色
activeBackgroundColor - 活动选项卡的背景颜色
inactiveTintColor - 非活动标签的标签和图标颜色
inactiveBackgroundColor - 非活动标签的背景颜色
showLabel - 是否显示标签的标签,默认为true
style - 标签栏的样式对象
labelStyle - 标签标签的样式对象
tabStyle - 标签的样式对象


tabBarOptions for (Android上的默认标签栏)TabBarTop
activeTintColor - 活动标签的标签和图标颜色
inactiveTintColor - 非活动标签的标签和图标颜色
showIcon - 是否显示标签的图标,默认值为false
showLabel - 是否显示标签的标签,默认为true
upperCaseLabel - 是否使标签大写,默认为true
pressColor - 材质波纹颜色(Android> = 5.0)
pressOpacity - 按压标签的不透明度(iOS和Android <5.0 only)
scrollEnabled - 是否启用可滚动选项卡
tabStyle - 标签的样式对象
indicatorStyle - 标签指示器的样式对象(选项卡底部的行)
labelStyle - 标签标签的样式对象
iconStyle - 标签图标的样式对象
style - 标签栏的样式对象

 

小技巧

1.去掉安卓下的下划线,设置:tabBarOptions => indicatorStyle:{ height: 0 };

2.底部导航在导航最上方添加一条分割线,设置:tabBarOptions => style => borderTopWidth: 0.5, borderTopColor: '#ccc';

3.导航安卓图标和文字间隙比较大,手动调整小设置:tabBarOptions => labelStyle => margin: 0;

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/547065.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Linux QT5.12 一种整体界面字体设置的方法及设置PlainTextEdit组件的字体大小方法

1.在Linux QT5.12开发界面时,经常会涉及到界面字体大小的设置&#xff0c;默认字体一般比较小,解决方法如下: 在main函数中添加代码: // // 一种整体界面字体设置的方法&#xff1a; QFont font a.font(); font.setPointSize(14); a.setFont(font); // 2.在L…

Win7电脑,无法把文件保存到桌面上?

今天有用户反映重装了Win7后&#xff0c;文件无法另存到桌面上&#xff0c;解决方法如下&#xff1a;1、在任何地方打开资源管理器&#xff0c;按<Alt><F>键打开资源管理器的菜单&#xff1b;2、选择“工具”的“文件夹选项”&#xff0c;在“导航窗格”里选上“显…

ReactNative常用组件汇总

导航组件react-navigation: https://github.com/react-community/react-navigation 网络请求asios: https://github.com/mzabriskie/axios 设备信息react-native-device-info: https://github.com/rebeccahughes/react-native-device-info 缓存使用react-native-storage: https…

Yolov5训练自己的数据集之制作数据集

在VOC 2018文件夹下有五个文件夹&#xff0c;搜集好的图片放在JPEGImages文件夹下&#xff1b;标注后数据保存在Annotations文件夹下&#xff1b;labels文件夹在数据集的训练时用到&#xff1b;在ImageSets文件夹下有下面三个文件夹&#xff0c;在Main文件夹中有一个train.txt文…

ReactNative布局样式总结

flex number 用于设置或检索弹性盒模型对象的子元素如何分配空间 flexDirection enum(row, row-reverse ,column,column-reverse) flexDirection属性决定主轴的方向&#xff0c;默认是“column”&#xff1a; row&#xff1a;主轴为水平方向&#xff0c;起点在左端row-rev…

Android 线程池对象-ThreadPoolExecutor浅析

本人最近在已经在91&#xff0c;百度应用等渠道上线的个人应用——铃声酷的代码里用到了ThreadPoolExecutor这一线程池对象去处理并发&#xff0c;个人感觉相当的给力啊&#xff01;它是并发实用程序开放源码库 util.concurrent&#xff0c;它包括互斥、信号量、诸如在并发访问…

Linux kubuntu x64系统下解决QT5.12编辑菜单和工具栏不显示图标问题

Linux kubuntu x64系统下发现QT5.12在设计视图下编辑菜单和工具栏显示图标,但是编译运行后发现菜单和工具栏不显示图标,如下图: 我的解决办法是: 1.在QT项目中,菜单和工具栏图标一定要添加到项目资源文件中(在资源编辑器中Add Prefix后,再添加文件,关闭资源编辑器后自动将图标…

PyTorch系列 (二): pytorch数据读取自制数据集并

PyTorch系列 (二): pytorch数据读取 PyTorch 1: How to use data in pytorch Posted by WangW on February 1, 2019 参考&#xff1a; PyTorch documentationPyTorch 码源 本文首先介绍了有关预处理包的源码&#xff0c;接着介绍了在数据处理中的具体应用&#xff1b; 1 P…

nodejs+nginx获取真实ip

nodejs nginx获取真实ip分为两部分&#xff1a; 第一、配置nginx&#xff1b;第二、通过nodejs代码获取&#xff1b; 其他语言也是一样的&#xff0c;都是配置nginx之后&#xff0c;在http头里面获取“x-forwarded-for”. 第一、配置nginx location / {proxy_set_header Ho…

【OSChina-MoPaaS应用开发大赛】豪美创新后台业务管理系统

2019独角兽企业重金招聘Python工程师标准>>> 应用名称&#xff1a;豪美创新后台业务管理系统 应用URL地址&#xff1a;http://tyz.sturgeon.mopaas.com/admin/index.html 登录&#xff1a;admin/admin 投票地址&#xff1a;http://www.oschina.net/mopaas-app-co…

QT5更改应用程序图标

1.准备好.ico的图片放在工程目录下&#xff0c;并添加到项目的资源文件中 2.在项目配置.pro文件中添加一下内容 RC_ICONS AppIcon.icoAppIcon为你的ico图片名字 3.在可视化设计文件.ui中选择主窗口&#xff0c;将其属性中的windowIcon一项右侧下三角单击&#xff0c;从“选择…

python List中元素两两组合

python List中元素两两组合 import itertools aa [a, b, c] bb list(itertools.permutations(aa, 2)) print(bb) print("######################") cc list(itertools.combinations(aa, 2)) print(cc) 话不多说&#xff0c;运行结果解释一些 任何程序错误&…

xcode编译报错unknown error -1=ffffffffffffffff Command /bin/sh failed with exit code 1

升级完xcode9.1之后&#xff0c;编译项目出现如下错误&#xff1a; CI今日构建时报出如下错误&#xff1a; /Users/xxx/Library/Developer/Xcode/DerivedData/Snowball-ebllohyukujrncbaldsfojfjxwep/Build/Intermediates.noindex/ArchiveIntermediates/ProjectName/Installat…

android button的selector

实现按钮的selector <?xml version"1.0" encoding"utf-8"?><selector xmlns:android"http://schemas.android.com/apk/res/android"><item android:drawable"drawable/common_topbar_route_bus_pressed" android:sta…

Windows Qt5下用QAxObject快速读写Excel指南

Qt Windows 下快速读写Excel指南 很多人搜如何读写excel都会看到用QAxObject来进行操作&#xff0c;很多人试了之后都会发现一个问题&#xff0c;就是慢&#xff0c;非常缓慢&#xff01;因此很多人得出结论是QAxObject读写excel方法不可取&#xff0c;效率低。 后来我曾试过…

python opencv过滤红色

OpenCV简易视频处理框架OpenCV主要色彩空间OpenCV的位操作方法 找出视频中红色物体 import cv2 import numpy as npdef filter_out_black(src_frame):if src_frame is not None:hsv cv2.cvtColor(src_frame, cv2.COLOR_BGR2HSV)lower_red np.array([0, 0, 0])upper_red np.…

#39;boost/iterator/iterator_adaptor.hpp#39; file not found之xcode生成时报错的解决方案

xcode生成rn&#xff08;0.49.3&#xff09;项目的时候出现“boost/iterator/iterator_adaptor.hpp file not found之xcode”报错。 原因&#xff1a; /Users/xxx/.rncache 中 boost_1_63_0.tar.gz&#xff0c; double-conversion-1.1.5.tar.gz&#xff0c; folly-2016.09.26.…

经典面试题SALES TAXES思路分析和源码分享

题目&#xff1a; SALES TAXESBasic sales tax is applicable at a rate of 10% on all goods, except books, food, and medical products that are exempt. Import duty is an additional sales tax 除书籍 食品 药品外其他商品基本税为10%。进口税附加5%&#xff0c;不免税。…

Snipaste在Window运行后遇到提示计算机中丢失 api-ms-win-crt-runtime-l1-1-0.dll 错误

故障排除 以下为运行 Snipaste 可能遇到的错误及其解决方案。 Windows 运行后遇到提示计算机中丢失 api-ms-win-crt-runtime-l1-1-0.dll 错误 请根据你操作系统的版本&#xff08;32位/64位&#xff09;&#xff0c;下载并安装相应的微软 Visual C 2015 可再发行组件包: 32…