务实地检测 MIUI/Xiomi 设备中软导航栏的可用性?

Detect soft navigation bar availability in MIUI/Xiomi device pragmatically?

这个问题专门针对带有 MIUI 的 Xiomi 设备的问题。

如何检测全屏模式(手势)或导航按钮(软导航)是否被选中?

我尝试了一些解决方案,但在其他设备上确实有效,但在 Xiomi 或 MIUI 上无效。

我已经在 SO 上尝试过这个解决方案,所以请提供另一个解决方案。

1

public boolean hasNavBar (Resources resources) 
{
    int id = resources.getIdentifier("config_showNavigationBar", "bool", "android");
    return id > 0 && resources.getBoolean(id);
}

2

boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
boolean hasHomeKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_HOME);

if (hasBackKey && hasHomeKey) {
    // no navigation bar, unless it is enabled in the settings
} else {
    // 99% sure there's a navigation bar
}

3

View decorView = getWindow().getDecorView();
decorView.setOnSystemUiVisibilityChangeListener
        (new View.OnSystemUiVisibilityChangeListener() {
            @Override
            public void onSystemUiVisibilityChange(int visibility) {
                if ((visibility & View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0) {
                    // TODO: The navigation bar is visible. Make any desired
                    // adjustments to your UI, such as showing the action bar or
                    // other navigational controls.
                } else {
                    // TODO: The navigation bar is NOT visible. Make any desired
                    // adjustments to your UI, such as hiding the action bar or
                    // other navigational controls.
                }
            }
        });

知道如何知道导航栏当前是否可见吗?

我也试着计算了实际宽度和可用宽度,好像MIUI总是返回导航栏的预留with

谢谢。

无需检测全屏软输入导航栏是否可见或隐藏activity您可以创建样式并将样式应用于activity。

 <style name="Theme" parent="Theme.AppCompat.Light.NoActionBar">
         <item name="android:windowDrawsSystemBarBackgrounds" tools:targetApi="lollipop">false</item>
         <item name="android:windowTranslucentStatus">true</item>
     </style>

并在 activity 中添加以下行:

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
                    WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        }

这导致您停止软输入导航的布局重叠。

我也遇到了同样的问题。我找到了 2 个解决方案:

  1. This 解决方案适用于 Android Q.

    以上版本
    public static int isEdgeToEdgeEnabled(Context context) {
        Resources resources = context.getResources();
        int resourceId = resources.getIdentifier("config_navBarInteractionMode", "integer", "android");
        if (resourceId > 0) {
            return resources.getInteger(resourceId);
        }
        return 0;
    }
    

如果此方法 returns 2 则启用全屏模式。

2. 解决方案在 Android Q 及以下对我有效:

ViewCompat.setOnApplyWindowInsetsListener(
    findViewById(android.R.id.content)
) { _: View?, insets: WindowInsetsCompat ->
    navigationBarHeight = insets.systemWindowInsetBottom
    insets
}

我自己用了2种方法