首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否以程序方式检测onePlusOne设备中软导航栏的可用性?

是否以程序方式检测onePlusOne设备中软导航栏的可用性?
EN

Stack Overflow用户
提问于 2016-06-08 17:54:22
回答 5查看 3.2K关注 0票数 5

我需要检查设备是否有软导航栏,我遵循了here的建议。

它工作得很好,除了在onePlus设备上,出于某种原因,这段代码:

代码语言:javascript
复制
int id = resources.getIdentifier("config_showNavigationBar", "bool", android");
    return id > 0 && resources.getBoolean(id);

返回false,但会显示软导航栏。

你知道怎样才能得到正确的结果吗?

我不喜欢计算实际宽度和可用宽度,这似乎是昂贵的操作。

谢谢。

EN

回答 5

Stack Overflow用户

发布于 2016-10-19 17:50:40

请参阅this answer。然而,没有办法百分之百地确定。

代码语言:javascript
复制
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
}

编辑

另一种方法

代码语言:javascript
复制
public boolean hasNavBar (Resources resources) {
    int id = resources.getIdentifier("config_showNavigationBar", "bool", "android");
    return id > 0 && resources.getBoolean(id);
}
票数 2
EN

Stack Overflow用户

发布于 2016-10-19 19:31:48

是的,你可以试试这个:

代码语言:javascript
复制
    WindowManager mgr = (WindowManager) getSystemService(WINDOW_SERVICE);
            boolean hasSoftKey = Utils.hasSoftKeys(mgr, NPTApplication.this);

public static boolean hasSoftKeys(WindowManager windowManager, Context c) {
        boolean hasSoftwareKeys = true;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            Display d = windowManager.getDefaultDisplay();

            DisplayMetrics realDisplayMetrics = new DisplayMetrics();
            d.getRealMetrics(realDisplayMetrics);

            int realHeight = realDisplayMetrics.heightPixels;
            int realWidth = realDisplayMetrics.widthPixels;

            DisplayMetrics displayMetrics = new DisplayMetrics();
            d.getMetrics(displayMetrics);

            int displayHeight = displayMetrics.heightPixels;
            int displayWidth = displayMetrics.widthPixels;

            hasSoftwareKeys = (realWidth - displayWidth) > 0 || (realHeight - displayHeight) > 0;
        } else {
            boolean hasMenuKey = ViewConfiguration.get(c).hasPermanentMenuKey();
            boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
            hasSoftwareKeys = !hasMenuKey && !hasBackKey;
        }
        return hasSoftwareKeys;
    }
票数 1
EN

Stack Overflow用户

发布于 2016-10-19 04:48:49

no不会这样工作,您必须计算大小

所使用的方法在此SO答案中有详细说明;

How to get height and width of navigation bar programmatically

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37699161

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档