【问题标题】:Detect soft navigation bar availability in MIUI/Xiomi device pragmatically?实用地检测 MIUI/小米设备中的软导航栏可用性?
【发布时间】:2019-03-16 12:39:55
【问题描述】:

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

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

我尝试了一些解决方案,但在其他设备上确实有效,但在小米或 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总是返回导航栏的reserved with。

谢谢。

【问题讨论】:

  • 你有没有办法检查这个?我正在尝试做同样的事情
  • @moyo 即使我也有同样的问题

标签: java android xiaomi miui


【解决方案1】:

对于全屏活动,无需检测软输入导航栏是可见还是隐藏,您可以创建样式并将样式应用于活动。

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

并在活动中添加以下行:

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

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

【讨论】:

  • 抱歉,这不能回答问题
【解决方案2】:

我也有同样的问题。我找到了 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;
    }
    

如果此方法返回 2,则启用全屏模式。

2.This 解决方案适用于我在 Android Q 之上和之下:

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

我自己用了两种方法

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-06
    • 2018-11-14
    • 1970-01-01
    • 1970-01-01
    • 2016-12-29
    • 1970-01-01
    相关资源
    最近更新 更多