Android 获取底部导航条高度

获取设备底部导航条高度

/**
 * 获取底部导航条高度
 */
private fun getNavigationBarHeight(): Int {
        if (!isNavigationBarShow()) {
            return 0
        }
        val resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android")
        //获取NavigationBar的高度
        return resources.getDimensionPixelSize(resourceId)
    }

判断底部导航条是否显示

/**
 * 底部导航是否显示
 */
private fun isNavigationBarShow(): Boolean {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            val display = windowManager.defaultDisplay
            val size = Point()
            val realSize = Point()
            display.getSize(size)
            display.getRealSize(realSize)
            return realSize.y !== size.y
        } else {
            val menu = ViewConfiguration.get(this).hasPermanentMenuKey()
            val back = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK)
            return if (menu || back) false else true
        }
    }

隐藏键盘输入法

private fun hintKeyBoard() {
        //拿到InputMethodManager
        val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        //如果window上view获取焦点 && view不为空
        if (imm.isActive && currentFocus != null) {
            //拿到view的token 不为空
            if (currentFocus!!.windowToken != null) {
                //表示软键盘窗口总是隐藏,除非开始时以SHOW_FORCED显示。
                imm.hideSoftInputFromWindow(currentFocus!!.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
            }
        }
    }

tips:这个方法在全面屏上基本无效

猜你喜欢

转载自blog.csdn.net/qq_24523279/article/details/90700527
今日推荐