如果Android设备有软导航键,我怎么能以编程方式知道?

时间:2016-04-19 05:47:42

标签: android

我想在设备屏幕上显示导航栏上方的视图,否则显示在屏幕底部。

2 个答案:

答案 0 :(得分:2)

在您的活动或片段中使用此方法,以了解设备是否具有软导航栏。 然后,您可以根据方法调用中的要求执行代码。

像这样: -

     if (hasNavBar(getResources()))
        {
            //do your code here  
        }

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

答案 1 :(得分:0)

您可以尝试以下方法。它可以很好地工作:

public boolean hasNavBar (Resources resources) {
    boolean hasNavigationBar = true;
    int id = resources.getIdentifier("config_showNavigationBar", "bool", "android");
    if (id > 0) {
        hasNavigationBar = resources.getBoolean(id);
    }
    try {
        Class systemPropertiesClass = Class.forName("android.os.SystemProperties");
        Method m = systemPropertiesClass.getMethod("get", String.class);
        String navBarOverride = (String) m.invoke(systemPropertiesClass, "qemu.hw.mainkeys");
        if ("1".equals(navBarOverride)) {
            hasNavigationBar = false;
        } else if ("0".equals(navBarOverride)) {
            hasNavigationBar = true;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return hasNavigationBar;
}

solution from a chinese website