您必须使用自定义标签栏来实现这一点。
您可以在此处查看自定义标签栏的代码
function MyTabBar({ state, descriptors, navigation, position }) {
return (
<View style={{ flexDirection: 'row', backgroundColor: 'blue' }}>
{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,
});
if (!isFocused && !event.defaultPrevented) {
navigation.navigate(route.name);
}
};
const onLongPress = () => {
navigation.emit({
type: 'tabLongPress',
target: route.key,
});
};
return (
<TouchableOpacity
accessibilityRole="button"
accessibilityState={isFocused ? { selected: true } : {}}
accessibilityLabel={options.tabBarAccessibilityLabel}
testID={options.tabBarTestID}
onPress={onPress}
onLongPress={onLongPress}
style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Animated.Text
style={{
color: isFocused ? 'white' : 'grey',
marginVertical: 10,
}}>
{label}
</Animated.Text>
<View
style={{
width: '80%',
backgroundColor: isFocused ? 'white' : 'transparent',
height: 5,
marginTop: 'auto',
}}></View>
</TouchableOpacity>
);
})}
</View>
);
}
你必须像下面这样在你的导航器中使用它
<Tab.Navigator tabBar={(props) => <MyTabBar {...props} />}>
输出将是
你可以试试这个小吃
https://snack.expo.io/am8bsZI-4
您可以根据需要更改颜色、高度和边距。
其他选项是使用renderIndicator 或indicatorStyle,但它们不会正确采用宽度,因此以上将是最佳选择。