android 獲取虛擬鍵盤高度

判斷是否有虛擬鍵盤

    @SuppressLint("NewApi")
    public static boolean checkDeviceHasNavigationBar(Context activity) {

        //通過判斷設(shè)備是否有返回鍵、菜單鍵(不是虛擬鍵,是手機屏幕外的按鍵)來確定是否有navigation bar
        boolean hasMenuKey = ViewConfiguration.get(activity)
                .hasPermanentMenuKey();
        boolean hasBackKey = KeyCharacterMap
                .deviceHasKey(KeyEvent.KEYCODE_BACK);

        if (!hasMenuKey && !hasBackKey) {
            // 做任何你需要做的,這個設(shè)備有一個導(dǎo)航欄
            return true;
        }
        return false;
    }

獲取虛擬鍵盤高度

   public static int getNavigationBarHeight(Context activity) {
        Resources resources = activity.getResources();
        int resourceId = resources.getIdentifier("navigation_bar_height",
                "dimen", "android");
        //獲取NavigationBar的高度
        int height = resources.getDimensionPixelSize(resourceId);
        return height;
    }

有虛擬鍵盤的手機,獲取到的手機屏幕的高度包括了虛擬鍵盤的高度,在進行一些計算的時候可能需要將虛擬鍵盤的高度減掉。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容