更改React导航材料顶部标签中的活动标签背景颜色

时间:2020-10-29 13:11:30

标签: react-native react-navigation-v5

在使用React Navigation中的material-top-tabs时如何更改活动选项卡的背景颜色?

这是现在的样子:

这是我的代码:

import React from 'react'
import { createMaterialTopTabNavigator } from '@react-navigation/material-top-tabs';
import CrurrentOrders from './CrurrentOrders';
import PastOrders from './PastOrders';

const Tab = createMaterialTopTabNavigator();

const OrdersTabs = () => {
    return (
        <Tab.Navigator
            initialRouteName='CrurrentOrders'
            backBehavior='initialRoute'
            tabBarPosition='top'
            swipeEnabled={true}
            swipeVelocityImpact={0.2}
            springVelocityScale={0}
            sceneContainerStyle={{ backgroundColor: '#d1dfff', margin: 10, borderRadius: 20 }}
            style={{ backgroundColor: '#ffffff' }}
            tabBarOptions={{
                activeTintColor: '#ffffff',
                inactiveTintColor: '#ffffff',
                showIcon: true,
                pressColor: '#856',
                scrollEnabled: false,
                tabStyle: { backgroundColor: '#36A7E7', borderRadius: 30, margin: 12, justifyContent: 'center', alignContent: 'center' },
                indicatorStyle: { backgroundColor: '#987', opacity: 0.05 },
                style: { backgroundColor: '#ffffff', borderRadius: 30, margin: 24, height: 72, width: '90%' },
                labelStyle: { fontSize: 14 },

            }}
        >
            <Tab.Screen
                name="CrurrentOrders"
                component={CrurrentOrders}
                options={{
                    title: 'Awsome app',
                    tabBarTestID: 'werwer',
                }}

            />
            <Tab.Screen
                name="PastOrders"
                component={PastOrders}
            />
        </Tab.Navigator>
    )
}

export default OrdersTabs

3 个答案:

答案 0 :(得分:0)

像此处定义的material-top-tab-navigator/更改了活动标签的图层颜色(文本颜色)

对于背景更改,您可以执行类似的操作

function MyTabBar({ state, descriptors, navigation, position }) {
  return (
    <View style={{ flexDirection: 'row' }}>
      {state.routes.map((route, index) => {
        const { options } = descriptors[route.key];
        const label =
          options.tabBarLabel !== undefined
            ? options.tabBarLabel
            : options.title !== undefined
            ? options.title
            : route.name;

        const isFocused = state.index === index;

        const onPress = () => {
          const event = navigation.emit({
            type: 'tabPress',
            target: route.key,
            canPreventDefault: true,
          });

          if (!isFocused && !event.defaultPrevented) {
            navigation.navigate(route.name);
          }
        };

        const inputRange = state.routes.map((_, i) => i);
        const bgColor = isFocused ? "blue" : "black"; <!-- Here is bg color -->

        return (
          <TouchableOpacity
            accessibilityRole="button"
            accessibilityState={isFocused ? { selected: true } : {}}
            accessibilityLabel={options.tabBarAccessibilityLabel}
            testID={options.tabBarTestID}
            onPress={onPress}
            onLongPress={onLongPress}
            style={{ flex: 1 }}
          >
            <View style={{backgroundColor: bgColor}}>
                <Animated.Text style={{ opacity }}>
                    {label}
                </Animated.Text>
       </View>  
          </TouchableOpacity>
        );
      })}
    </View>
  );
}

答案 1 :(得分:0)

谢谢您的链接,该hack是带有activeTintColor的indicatorStyle

tabBarOptions={{
            activeTintColor: '#ffffff',
            inactiveTintColor: '#36A7E7',
            showIcon: true,
            pressColor: '#9BC9E2',
            scrollEnabled: false,
            tabStyle: {
                borderRadius: 30,
                margin: 12,
                justifyContent: 'center',
                alignContent: 'center'
            },
            indicatorStyle: {
                backgroundColor: '#36A7E7',
                height: '80%',
                borderRadius: 30,
                marginBottom: 8,
                marginLeft: 12,
                width: '45%'
            },
            style: {
                backgroundColor: '#ffffff',
                borderRadius: 36,
                margin: 24,
                height: 76,
                width: '90%'
            },
            labelStyle: { fontSize: 14 },

        }}

enter image description here

答案 2 :(得分:0)

我遇到了这个确切的挑战,并且能够通过在 indicatorStyle 选项中指定背景颜色和全高来解决它:

import * as React from 'react';
import { createMaterialTopTabNavigator } from '@react-navigation/material-top-tabs';
import HomeScreen from './HomeScreen';
import AboutScreen from './AboutScreen';
import ContactScreen from './ContactScreen';

const Tab = createMaterialTopTabNavigator();

export default function TopTabs() {
  const tabBarOptions = {
    activeTintColor: 'white',
    inactiveTintColor: 'black',
    indicatorStyle: { backgroundColor: 'red', height: '100%' },
    pressOpacity: 1,
  }

  return (
    <Tab.Navigator tabBarOptions={tabBarOptions}>
      <Tab.Screen name="Home" component={HomeScreen} />
      <Tab.Screen name="About" component={AboutScreen} />
      <Tab.Screen name="Contact" component={ContactScreen} />
    </Tab.Navigator>
  );
}