react-native - 有没有办法让 `react-navigation` 的 `indicator` 适合标签?
问题描述
我想适合indicator
导航栏的选项卡。但它只适合第一个标签。对于所有其他选项卡,右侧的指示器会滑动一点。我在导航部分使用margins
了左右。style
下图显示了该场景。
这是导航组件的代码
const Navigation = createMaterialTopTabNavigator(
{
S: Screen1,
S2: Screen2,
S3: Screen3,
},
{
swipeEnabled: false,
tabBarOptions: {
activeTintColor: "white",
inactiveTintColor: "blue",
upperCaseLabel: false,
scrollEnabled: false,
inactiveBackgroundColor: "white",
indicatorStyle: {
height: null,
top: 0,
backgroundColor: 'red',
width:110,
},
style: {
marginLeft: 15,
marginRight:15,
borderWidth: 1,
height: 30,
borderColor: "blue",
backgroundColor: "white",
},
tabStyle: {
borderWidth:1,
borderColor:"blue",
justifyContent: "center"
},
labelStyle: {
marginTop: -4
}
}
}
);
export default createAppContainer(Navigation);
我怎样才能解决这个问题 ?
解决方案
问题是你的marginLeft
和marginRight
通过你的整个tabBar传播。
您可以通过引入以下内容来解决此问题:
import { Dimensions } from 'react-native';
const width = Dimensions.get('window').width;
const tabBarWidth = width - 30; // Subtract margins from your screen width. In your case 2*15= 30
并更新您的 tabBarOptions:
tabBarOptions: {
activeTintColor: "white",
inactiveTintColor: "blue",
upperCaseLabel: false,
scrollEnabled: false,
inactiveBackgroundColor: "white",
indicatorStyle: {
height: null,
top: 0,
backgroundColor: 'red',
//width:110, remove width here
},
style: {
marginTop: 60, // quick hack for iphone X
marginLeft: 15,
marginRight:15,
borderWidth: 1,
height: 30,
borderColor: "blue",
backgroundColor: "white",
},
tabStyle: {
borderWidth:1,
borderColor:"blue",
justifyContent: "center",
width: tabBarWidth/4, // divided by amount of screens you have
},
labelStyle: {
marginTop: -4
}
}
如您所见,结果也适用于例如 4 个选项卡:
推荐阅读
- powerbi - Power BI 仅计算重复 ID 的最后一个值的总和
- c - C中的内存对齐:返回地址中的偏移量是如何考虑的?
- python - 类型错误:__init__() 缺少 1 个必需的位置参数:'msg' 每次我尝试在 macbook 上下载 anaconda3
- ruby-on-rails - 如何绕过带有 RSPEC 的 Rails 控制器中的 recaptcha 验证?
- r - 如何从 x、y、z 使用光线着色器包进行 3D 表面处理
- java - 如何更改 ArrayList 中对象的显示名称
- hibernate - 休眠。QuerySyntaxException:用户未映射,即使它是
- c# - 在数据注释中使用枚举
- php - PHP中的websocket是否存在踩踏?
- reactjs - 提交后无法清除输入,文本参数返回空