我在一个显示 Snackbar
的应用程序上工作.
这个Snackbar
在我的 MainActivity
中使用以下代码可以正常工作:
Snackbar snackbar = Snackbar.make(myLayout, "My snackbar", Snackbar.LENGTH_LONG)
.setBackgroundTint(myColor)
.setTextColor(ContextCompat.getColor(this, R.color.black));
[...]
snackbar.show();
这是显示:它适用于每个方向的每个屏幕和不同的 Android 版本。只有一种配置会出错:当设备使用手势导航时。 (这么多新手机)
在这种情况下,
Snackbar
显示 Snackbar
之间的差距和屏幕底部是这样的:我在网上没有找到任何解释,但我自己找到了这个。
通过打开
Show layout bounds
在开发者选项中,我们可以观察到“经典”导航栏看起来仍然在这里,就好像可见性设置为 View.INVISIBLE
我们可以期待View.GONE
无论屏幕如何。所以,当我尝试显示
Snackbar
,它就出现在这个“隐形导航栏”的顶部。这是一些带有和不带有手势导航的布局边界的屏幕(在主菜单中):
这是使用不同的手机和品牌(Pixel、Samsung ...)甚至模拟器进行的测试。
有正常行为吗?我怎样才能抑制这个差距?
最佳答案
对我来说设置GestureInsetBottomIgnored
至true
修复了问题:
snackbar.setGestureInsetBottomIgnored(true);
来自 Android Docs :Sets whether this bottom bar should adjust it's position based on the system gesture area on Android Q and above. Note: the bottom bar will only adjust it's position if it is dismissable via swipe (because that would cause a gesture conflict), gesture navigation is enabled, and this gestureInsetBottomIgnored flag is false.
关于android - 带有手势导航的错误 snackbar 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63705222/