天天看點

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:這個方法在全面屏上基本無效