首页 > 解决方案 > Android P 的 Android 导航栏可见性 - 检测基于手势的导航

问题描述

如何检测导航栏(对于无 Bazel 设备)是否可见?我尝试使用Resource IdentifierSystem UI FlagKeyCharacterMap为导航栏使用不同的解决方案,但它对我不起作用。在Andorid P中,手势导航可用,它将隐藏导航栏,导航功能将使用该手势运行。有什么解决方案可以检测到这一点吗?

标签: androidnavigationandroid-9.0-pie

解决方案


我使用两种手势导航方法在小米设备上测试了这段代码,对我来说它正在工作:

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。


推荐阅读