如何检测导航栏(用于无 Bazel 设备)是否可见?我尝试使用 Resource Identifier、System UI Flag 和 KeyCharacterMap 为导航栏提供不同的解决方案,但它对我不起作用。在 Andorid P 中,手势导航可用,它将隐藏导航栏,导航功能将使用该手势运行。有什么解决方案可以检测到这一点吗?
最佳答案
我使用两种手势导航方法在小米设备上测试了这段代码,对我来说它是有效的:
Point p = new Point();
getWindow().getWindowManager().getDefaultDisplay().getRealSize(p);
ConstraintLayout container = findViewById(your activity main view (e.g linearLayout));
final TypedArray styledAttributes = getTheme().obtainStyledAttributes(
new int[]{android.R.attr.actionBarSize});
int mActionBarSize = (int) styledAttributes.getDimension(0, 0);
int height = container.getMeasuredHeight();
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
int result = 0;
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
if (height + mActionBarSize + result >= p.y) {
//you have fullscreen gesture navigation
}else{
//navigation bar or similiar
}
之所以可行,是因为该应用现在可以在屏幕上占据更大的尺寸,因此仅当手势导航方法不使“导航栏”成为背景时,它才有效。
请务必在 main 已加载且可见时使用它,否则它将返回 0。
关于Android P 的 Android 导航栏可见性 - 检测基于手势的导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54942371/