导航一直是app开发中比较重要的一个组件,reactnative提供了两种导航组件供我们使用,分别是:navigatorios和navigator,但是前者只能用于ios平台,后者在reactnative0.44版本以后已经被移除了。
好在有人提供了更好的导航组件,就是我们今天要讲的react-navigation,并且reactnative官方更推荐我们使用此组件。
本篇文章只讲解基础用法,如果你想了解更多,请戳这里->。
简介
react-navigation主要包括导航,底部tab,顶部tab,侧滑等,分别为:
我们今天主要讲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:
tabnavigatorconfig
tabbaroptions for (ios上的默认标签栏)tabbarbottom
tabbaroptions for (android上的默认标签栏)tabbartop
小技巧
1.去掉安卓下的下划线,设置:tabbaroptions => indicatorstyle:{ height: 0 };
2.底部导航在导航最上方添加一条分割线,设置:tabbaroptions => style => bordertopwidth: 0.5, bordertopcolor: '#ccc';
3.导航安卓图标和文字间隙比较大,手动调整小设置:tabbaroptions => labelstyle => margin: 0;
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问, 点击进行留言回复!!
轻松解决 org.apache.taglibs.standard.tlv.JstlCoreTLV 困惑
vert实践五——Json?Protocol Buffer?FlatBuffers?
[基于tensorflow的人脸检测] 基于神经网络的人脸检测8——验证训练好的神经网络
selenium + ajax抓取英雄联盟全部英雄的详细信息及多线程保存全部皮肤图片到本地
网友评论