判斷是否有虛擬鍵盤
@SuppressLint("NewApi")
public static boolean checkDeviceHasNavigationBar(Context activity) {
//通過判斷設備是否有返回鍵悲酷、菜單鍵(不是虛擬鍵,是手機屏幕外的按鍵)來確定是否有navigation bar
boolean hasMenuKey = ViewConfiguration.get(activity)
.hasPermanentMenuKey();
boolean hasBackKey = KeyCharacterMap
.deviceHasKey(KeyEvent.KEYCODE_BACK);
if (!hasMenuKey && !hasBackKey) {
// 做任何你需要做的,這個設備有一個導航欄
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;
}
有虛擬鍵盤的手機仍源,獲取到的手機屏幕的高度包括了虛擬鍵盤的高度,在進行一些計算的時候可能需要將虛擬鍵盤的高度減掉舔涎。