如何检测导航栏(对于没有Bazel的设备)是否可见?我尝试使用资源标识符,系统UI标志和KeyCharacterMap等不同的解决方案来处理导航栏,但对我来说均无效。在Android 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
}
这是因为应用程序现在可以在屏幕上占用更大的空间,所以它只在手势导航方法不使“导航栏”背景时才起作用。
请确保在主界面已经加载并可见时使用它,否则它将返回0。