gpt4 book ai didi

javascript - 如何删除 react 导航顶部栏中选项卡之间的空白

转载 作者:行者123 更新时间:2023-12-04 15:29:58 25 4
gpt4 key购买 nike

如何删除 react-navigation 顶部栏中选项卡之间的空白。所以应该发生的是标签大小应该根据屏幕大小进行调整。

这就是我的导航目​​前的样子。

enter image description here

您可以看到选项卡非常大。他们占用了太多空间。我怎样才能得到这些空格,以便选项卡只占用足够的大小以适应选项卡名称。

我试过造型Tab.Navigator没有运气。例如,我尝试了以下方法:

<Tab.Navigator
initialRouteName="All"
tabBarOptions={{
scrollEnabled: true,
labelStyle: { padding: 0, margin: 0, border: 0 },
tabStyle: { padding: 0, margin: 0, border: 0 },
}}
lazy={true}
style={{ padding: 0, margin: 0, border: 0 }}
>

import React from "react";
import { createStackNavigator } from "@react-navigation/stack";
import { createMaterialTopTabNavigator } from "@react-navigation/material-top-tabs";

import Home from "../screens/Home";

const Tab = createMaterialTopTabNavigator();
const Stack = createStackNavigator();

const HomeTabNavigation = () => {
return (
<Tab.Navigator
initialRouteName="All"
tabBarOptions={{
scrollEnabled: true,
labelStyle: { padding: 0, margin: 0, border: 0 },
tabStyle: { padding: 0, margin: 0, border: 0 },
}}
lazy={true}
style={{ padding: 0, margin: 0, border: 0 }}
>
<Tab.Screen name="All" component={Home} />
<Tab.Screen name="Hot" component={Home} />
<Tab.Screen name="Winners" component={Home} />
</Tab.Navigator>
);
};

const HomeNavigation = () => {
return (
<Stack.Navigator>
<Stack.Screen name="HomeTabNavigation" component={HomeTabNavigation} />
</Stack.Navigator>
);
};

export default HomeNavigation;

最佳答案

就在几天前,我遇到了同样的问题。

这是我为您提供的解决方案:

import React from "react";
import { useWindowDimensions } from "react-native"; // <-- add this hook
import { createStackNavigator } from "@react-navigation/stack";
import { createMaterialTopTabNavigator } from "@react-navigation/material-top-tabs";

import Home from "../screens/Home";

const Tab = createMaterialTopTabNavigator();
const Stack = createStackNavigator();

const tabsConfig = () => {
const { width } = useWindowDimensions()
return {
lazy: true,
tabBarOptions: {
showLabel: true,
tabStyle: {
// here you can set the tab width , in this case , 3 tabs , width / 3
width: width / 3,
justifyContent: 'center',
alignItems: 'center',
alignSelf: 'center',
},
indicatorStyle: {
borderWidth: 1,
borderColor: 'red',
}
},
}
}

const HomeTabNavigation = () => {
return (
<Tab.Navigator {...tabsConfig()}>
<Tab.Screen name="All" component={Home} />
<Tab.Screen name="Hot" component={Home} />
<Tab.Screen name="Winners" component={Home} />
</Tab.Navigator>
);
};

const HomeNavigation = () => {
return (
<Stack.Navigator>
<Stack.Screen name="HomeTabNavigation" component={HomeTabNavigation} />
</Stack.Navigator>
);
};

export default HomeNavigation;

关于javascript - 如何删除 react 导航顶部栏中选项卡之间的空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61418492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com