【问题标题】:Invisible soft navigation bar on some devices某些设备上不可见的软导航栏
【发布时间】:2018-11-14 08:11:21
【问题描述】:

我想知道设备是否有软导航栏。为此,我使用以下代码:

        val hasNavBar = resources.getIdentifier("config_showNavigationBar", "bool", "android")
        if(hasNavBar > 0 && resources.getBoolean(hasNavBar)){

            val resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android")
            if (resourceId > 0) {
                ApiHelper.navHeight = resources.getDimensionPixelSize(resourceId)
            }
        }

但是在一些没有软导航栏的设备上,resources.getDimensionPixelSize(resourceId) 给出了一些高度,hasNavBar 是真的。

出现此问题的设备是 Redmi Note 4 Pro。不知道其他设备有没有同样的问题。

【问题讨论】:

    标签: android button kotlin softkeys


    【解决方案1】:

    我找到了答案。

    stackOverflow 中有一些函数不起作用,但它起作用了。

    How to tell whether an android device has hard keys

    fun hasSoftKeys(windowManager: WindowManager): Boolean {
            var hasSoftwareKeys = true
    
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                val d = context.getWindowManager().getDefaultDisplay()
    
                val realDisplayMetrics = DisplayMetrics()
                d.getRealMetrics(realDisplayMetrics)
    
                val realHeight = realDisplayMetrics.heightPixels
                val realWidth = realDisplayMetrics.widthPixels
    
                val displayMetrics = DisplayMetrics()
                d.getMetrics(displayMetrics)
    
                val displayHeight = displayMetrics.heightPixels
                val displayWidth = displayMetrics.widthPixels
    
                hasSoftwareKeys = realWidth - displayWidth > 0 || realHeight - displayHeight > 0x<
            } else {
                val hasMenuKey = ViewConfiguration.get(context).hasPermanentMenuKey()
                val hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK)
                hasSoftwareKeys = !hasMenuKey && !hasBackKey
            }
            return hasSoftwareKeys
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-27
      相关资源
      最近更新 更多