【问题标题】:How can I know programatically if the android device has soft navigation keys or not?我如何以编程方式知道 android 设备是否有软导航键?
【发布时间】:2017-09-15 11:09:40
【问题描述】:

如果导航栏出现在设备屏幕上,我想在导航栏上方显示一个视图,否则在屏幕底部显示。

【问题讨论】:

  • 请不要把问题弄得不好,请适当解释
  • 虽然应该改写问题,但其他答案只能通过使用屏幕高度进行计算来解决问题,应该有一个更清洁的解决方案。 stackoverflow.com/questions/28406506/…

标签: android


【解决方案1】:

在您的活动或片段中使用此方法来了解设备是否具有软导航栏。 然后你可以在方法调用中按照要求做代码。

像这样:-

     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);
    }

【讨论】:

    【解决方案2】:

    您可以尝试以下方法。它可以完美运行:

    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

    【讨论】:

      猜你喜欢
      • 2013-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多