如何更改活动/选定选项卡的颜色?

How to change the color of the active / selected tab?

我希望颜色在未选中选项卡时为默认灰色,但在选中选项卡时为我的 tabBarColor 颜色。我找不到更改标签栏中标题颜色的方法。

我该怎么做?

这是我的代码:

Home:{
  screen: TabNavigator({
   Home: {
    screen: HomeScreen,
    navigationOptions: ({ navigation }) => ({
      title: 'Home',
      tabBarIcon: ({ tintColor, focused }) => (
        <Ionicons
        name={focused ? 'ios-home' : 'ios-home-outline'}
        size={26}
        style={{ color: focused ? `${tabBarColor}` : tintColor}}
        />
      ),
      //headerStyle: {backgroundColor: "#553A91"},
      //headerTitleStyle: {color: "#FFFFFF"},
      header: null,
    }),
  },
  Profile: {
    screen: ProfileScreen,
    navigationOptions: ({ navigation }) => ({
      title: 'Profile',
      tabBarIcon: ({ tintColor, focused }) => (
        <Ionicons
        name={focused ? 'ios-people' : 'ios-people-outline'}
        size={26}
        style={{ color: focused ? `${tabBarColor}` : tintColor }}
        />
      ),
      //headerStyle: {backgroundColor: "#553A91"},
      //headerTitleStyle: {color: "#FFFFFF"},
      header: null,
    }),
  },
}),
}

TabNavigator Docs中明确指出需要使用activeTintColor

activeTintColor: label and icon color of the active tab

示例:

const MyApp = TabNavigator({
  Home: {
    screen: MyHomeScreen,
  },
  Notifications: {
    screen: MyNotificationsScreen,
  },
}, {
  navigationOptions: ({navigation}) => ({
        tabBarIcon: ({focused}) => {
            ...
        }
  }),
  tabBarOptions: {
        activeTintColor: '#ffffff',
  },
});
  1. 定义变量路由,
  2. 为每个 添加道具侦听器,
  3. 使用变量路由来改变 tabBarLabel 的颜色。

示例:

const [route, setRoute] = useState('home');
 

        <Tab.Navigator>
           <Tab.Screen listeners={{
                  tabPress: (e) => {
                    setRoute('home');
                  },
                }}
                options={{
                  tabBarLabel: (
                    <Text
                      style={{
                        color: route === 'home' ? 'ACTIVE_COLOR' : 'INACTIVE_COLOR',
                      }}>
                      home
                    </Text>
                  ) />
    
    <Tab.Screen listeners={{
                  tabPress: (e) => {
                    setRoute('profile');
                  },
                }}
                options={{
                  tabBarLabel: (
                    <Text
                      style={{
                        color: route === 'profile' ? 'ACTIVE_COLOR' : 'INACTIVE_COLOR',
                      }}>
                      profile
                    </Text>
                  ) />
    </Tab.Navigator>

希望能帮到你