检查导航栏

Jon*_*nno 47 java layout android android-4.0-ice-cream-sandwich android-4.2-jelly-bean

我试图检查Android导航栏是否存在加载,以便我可以相应地调整布局,有没有人有任何建议?

这是我试图检测的导航栏: 在此输入图像描述

PS我到目前为止所发现的都是'糟糕'的方法来尝试删除吧,我不想这样做.

phi*_*ask 49

花了我一些时间,但我找到了比依赖hasPermanentMenuKey()更可靠的方式,这对于像HTC One这样没有菜单键但是有家用和后退键所以不需要的新手机不起作用(或者显示)软导航栏.要解决这个问题,请尝试以下代码来检查后退按钮:

boolean hasMenuKey = ViewConfiguration.get(context).hasPermanentMenuKey();
boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);

if(!hasMenuKey && !hasBackKey) {
    // Do whatever you need to do, this device has a navigation bar
}
Run Code Online (Sandbox Code Playgroud)

  • 在某些具有软键的设备上(如Galaxy Tab 2),KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK)返回true. (7认同)
  • 嗨,关于Lollipop hasBackKey即使对于使用NavBar的设备也是如此 - 任何想法如何解决这个问题? (4认同)
  • @zarej - 三星,你能做什么! (3认同)
  • 这对我不起作用,Nexus5为hasMenuKey和hasBackKey返回true,因此它认为没有navBar.(对于hasMenuKey和hasBackKey,Nexus7正确返回false).对我有用的更好的解决方案是:http://stackoverflow.com/a/29609679/2663916 (2认同)

Rud*_*dey 33

没有可靠的方法来检查导航栏.使用KeyCharacterMap.deviceHasKey您可以检查设备上是否存在某些物理键,但此信息不是很有用,因为具有物理键的设备仍然可以有导航栏.OnePlus One等设备或任何运行自定义ROM的设备在禁用物理键的设置中都有一个选项,并添加了一个导航栏.无法检查此选项是否已启用,并且deviceHasKey对于此选项禁用的键仍然返回true.

这是你能得到的最接近的:

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
}
Run Code Online (Sandbox Code Playgroud)

如果后退和主页按钮不是物理上存在于设备上,则它必须具有导航栏,因为否则用户将根本无法导航.但是,您永远无法100%确定这一点,因为制造商可能会实施deviceHasKey错误.


Pau*_*and 11

另一个解决方案(我的类UtilsUISystem的一部分)

    public static boolean hasNavBar (Resources resources)
    {
        //Emulator
        if (Build.FINGERPRINT.startsWith("generic"))
            return true;

        int id = resources.getIdentifier("config_showNavigationBar", "bool", "android");
        return id > 0 && resources.getBoolean(id);
    }
Run Code Online (Sandbox Code Playgroud)


Tad*_*Tad 9

这是一个结合了Pauland和Philask解决方案的快速解决方案.我担心我没有足够的设备来测试它是否适用于所有地方.我有兴趣听听别人的结果.

boolean hasNavBar(Context context) {
    Resources resources = context.getResources();
    int id = resources.getIdentifier("config_showNavigationBar", "bool", "android");
    if (id > 0) {
        return resources.getBoolean(id);
    } else {    // Check for keys
        boolean hasMenuKey = ViewConfiguration.get(context).hasPermanentMenuKey();
        boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
        return !hasMenuKey && !hasBackKey;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 我尝试使用这个,不幸的是在 Nexus 5 上它仍然不正确。即使导航栏存在,“resources.getBoolean(id)”也会返回 false。 (2认同)

joe*_*joe 5

您可以将此代码添加到活动的onCreate()方法中:

 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.
                }
            }
        });
Run Code Online (Sandbox Code Playgroud)


小智 5

我已经这样做了,它适用于我测试过的每台设备,甚至适用于模拟器:

public static boolean hasNavigationBar(Activity activity) {
    Rect rectangle = new Rect();
    DisplayMetrics displayMetrics = new DisplayMetrics();
    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rectangle);
    activity.getWindowManager().getDefaultDisplay().getRealMetrics(displayMetrics);
    return displayMetrics.heightPixels != (rectangle.top + rectangle.height());
}
Run Code Online (Sandbox Code Playgroud)

  • 最扎实的做法。因为这都是基于显示像素的 (2认同)