React-Navigation-TabNavigator add badge

在这里插入图片描述

npm -i --save react-native-tab-navigator


/**
 * 初始化页面
 */
import React from 'react'
import {StackNavigator, TabNavigator} from "react-navigation";
import WelcomePage from './WelcomePage';
import HomePage from './HomePage';
import {
  Image, View, Text, StyleSheet
} from "react-native";
import InteractionPage from "./InteractionPage";
import OnlinePage from "./OnlinePage";
import MyPage from "./MyPage";
import MainPage from './MainPage';
import Badge from "react-native-tab-navigator/Badge";


// tab 导航
const TabNavigatorDemo = TabNavigator(
  {
    MusicWarehouse: {
      screen: HomePage,
      navigationOptions: {
        title: '乐库',
      }
    },
    Interaction: {
      screen: InteractionPage,
      navigationOptions: {
        title: '互动',
      }
    },
    Online: {
      screen: OnlinePage,
      navigationOptions: {
        title: '直播',
      }
    },
    Me: {
      screen: MyPage,
      navigationOptions: {
        title: '我的',
      }
    },
  },
  {
    navigationOptions: ({navigation}) => ({
      tabBarIcon: ({focused, tintColor}) => {
        console.log(navigation);
        const {routeName} = navigation.state;
        if (routeName === 'MusicWarehouse') {
          return (<Image
            source={require('../../resource/images/ic_home_white_headset_60.png')}
            style={[{width: 24, height: 24}, {tintColor: tintColor}]}
          />)
        } else if (routeName === 'Interaction') {
          return (
            <View style={{width: 24, height: 24}}>
              <Badge children={11111} style={{position: 'absolute', left: 15, marginTop: -5}}/>
              <Image
                source={require('../../resource/images/ic_home_white_interaction_60.png')}
                style={[{width: 24, height: 24}, {tintColor: tintColor}]}
              /></View>)
        } else if (routeName === 'Online') {
          return (
            <View style={{width: 24, height: 24}}>
              <Badge children={3} style={{zIndex: 3,backgroundColor: 'red',position: 'absolute', left: 20, marginTop: -5}}/>
              <Image
                source={require('../../resource/images/ic_home_white_online_60.png')}
                style={[{width: 24, height: 24}, {tintColor: tintColor}]}
              /></View>)
        } else if (routeName === 'Me') {
          return <Image
            source={require('../../resource/images/ic_home_white_me_60.png')}
            style={[{width: 24, height: 24}, {tintColor: tintColor}]}
          />
        }
      },
    }),
    // tabBarComponent: TabBarBottom,
    tabBarPosition: 'bottom',
    tabBarOptions: {
      // 设置tab选中颜色
      activeTintColor: '#28beb4',
      // 未选中颜色
      inactiveTintColor: 'gray',
    },
    animationEnabled: false,
    swipeEnabled: false,
  }
);

// top 导航
const Setup = StackNavigator({
  Welcome: {
    screen: WelcomePage,
    navigationOptions: ({navigation}) => ({
      title: '欢迎页面'
    })
  },
  Home: {
    // screen: MainPage,
    screen: TabNavigatorDemo,
    navigationOptions: {
      // 不显示头
      header: null,
      // 字体颜色
      // headerTintColor: '#28beb4',
      // headerStyle:{
      //   backgroundColor: '#28beb4'
      // }
    }
  }
});


const styles = StyleSheet.create({
  badge: {
    position: 'absolute',
    left: 20,
    // backgroundColor:"red",
    // color:"#ffffff",
    // borderRadius:7,
    width: 15,
    height: 15,
    // overflow: 'hidden',
    // zIndex: 3
    fontSize: 10,
    color: '#fff',
    backgroundColor: 'rgb(0, 122, 255)',
    lineHeight: 15,
    textAlign: 'center',
    borderWidth: 1,
    borderColor: '#fefefe',
    borderRadius: 17 / 2,
    overflow: 'hidden',
  }
});

export default Setup;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值