简体   繁体   English

更改反应导航材料顶部选项卡中的活动选项卡背景颜色

[英]change active tab background color in react navigation material top tabs

How can I change the background color of the active tab while using material-top-tabs from React Navigation?使用 React Navigation 中的 material-top-tabs 时,如何更改活动选项卡的背景颜色?

Here's what things look like right now:这是现在的样子:

Here's my code:这是我的代码:

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

Like Defined here material-top-tab-navigator/ changed the layer color (text color) of the active tab像这里定义的material-top-tab-navigator/更改了活动选项卡的图层颜色(文本颜色)

For background change, you can do something like对于背景更改,您可以执行以下操作

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>
  );
}

Thank you for the link , the hack was in indicatorStyle with activeTintColor谢谢你的链接,hack 是使用 activeTintColor 的指标样式

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在此处输入图片说明

I had this exact challenge and was able to solve it by specifying the background color and full height in the indicatorStyle option:我遇到了这个确切的挑战,并且能够通过在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>
  );
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 反应导航更改材质顶部选项卡的背景颜色 - React navigation change background color of material top tabs 如何在@react-navigation/material-bottom-tabs 中移除活动按钮的背景色 - How to remove background color of active button in @react-navigation/material-bottom-tabs 在材料顶部选项卡中获取活动选项卡名称 - Get active Tab Name in material top tabs React Navigation:更改开关底部选项卡上的所有背景颜色 - React Navigation: Change all background color on switch bottom tabs @react-navigation/material-top-tabs 在选项卡上不起作用 android expo - @react-navigation/material-top-tabs not working on tab android expo 如何在反应原生导航中更改选定底部选项卡的背景颜色 - How to change the background color of selected bottom tab in react native navigation React-Native:在@react-navigation/material-bottom-tabs 中更改徽章颜色 - React-Native: Change badge color in @react-navigation/material-bottom-tabs 如何删除 React Navigation Material Bottom Tab Navigator 中的活动图标背景 - How to remove the active icon background in React Navigation Material Bottom Tab Navigator 是否有任何解决方案可以在使用反应导航时更改选项卡导航图标的颜色 - Is there any solution to change the color of icon of tab navigation when it's active using react navigation react-navigation/material-bottom-tabs 如何更改徽章颜色? - react-navigation/material-bottom-tabs how to change badge color?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM