【问题标题】:React-Native: Change badge color in @react-navigation/material-bottom-tabsReact-Native:在 @react-navigation/material-bottom-tabs 中更改徽章颜色
【发布时间】:2020-08-25 16:05:39
【问题描述】:

我可以更改材料底部标签中徽章的颜色吗?因为默认情况下它已经是红色的,我需要更改它。如果可以,我该怎么做?

...

import {createMaterialBottomTabNavigator} from '@react-navigation/material-bottom-tabs';

...
const MainBottomTab = createMaterialBottomTabNavigator();

...

    <MainBottomTab.Navigator
      initialRouteName="Home"
      activeColor="#C9C9C9"
      inactiveColor="#4523AF"
      barStyle={styles.menuBotao}>
...
      <MainBottomTab.Screen
        name="Publicações"
        component={PublicacaoNavication}
        options={{
          tabBarLabel: '',
          tabBarBadge: notificacoes !== undefined ? notificacoes.length : false, <-- I need to change the color
          tabBarIcon: ({color}) => <Icon name={'filing'} color={color} />,
        }}
      />

...

</MainBottomTab.Navigator>

...

【问题讨论】:

    标签: android react-native react-navigation


    【解决方案1】:

    您可以像这样覆盖默认主题:

    import { DefaultTheme, Provider as PaperProvider } from 'react-native-paper';
    
    // Create your custom theme and override the notification color
    const MyTheme = {
      ...DefaultTheme,
      colors: {
        ...DefaultTheme.colors,
        notification: 'blue',
      },
    };
    
    // Pass your theme to the PaperProvider wrapping your app
    const App = () => {
      return (
        <PaperProvider theme={MyTheme}>
          <NavigationContainer>
            // The rest of your content...
          </NavigationContainer>
        </PaperProvider>
      );
    };
    // ...
    

    【讨论】:

    • 对不起,我忘了说我没有使用NavigationContainer,只有const MainBottomTab = createMaterialBottomTabNavigator();,有没有机会这样做?
    • 如果我嵌套这两个(NavigationContainer 和 MainBottomTab.Navigator)我必须通过这个道具independent={true},因为我得到这个错误:Looks like you have nested a 'NavigationContainer' inside another. Normally you need only one container at the root of the app, so this was probably an error. If this was intentional, pass 'independent={true}' explicitely. Note that this will make the child navigators disconnected from the parent and you won't be able to navigate between them.,然后我试图通过主题道具togheter和没用。
    • 您应该有 1 个NavigationContainer 并将其放在组件树的顶部。因此,请确保您没有像错误告诉您的那样在彼此内部渲染 2。