尊重原創、尊重作者,轉載請标明出處:
http://blog.csdn.net/lnb333666/article/details/41821149
目前也沒有可靠的方法來檢查裝置上是否有導航欄。可以使用KeyCharacterMap.deviceHasKey來檢查裝置上是否有某些實體鍵,比如說菜單鍵、傳回鍵、Home鍵。然後我們可以通過存在實體鍵與否來判斷是否有NavigationBar(一般來說手機上實體鍵、NavigationBar共存).
public static int getNavigationBarHeight(Activity activity) {
Resources resources = activity.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height",
"dimen", "android");
//擷取NavigationBar的高度
int height = resources.getDimensionPixelSize(resourceId);
return height;
}
上面這段代碼,在絕大多數情況下都能擷取到NavigationBar的高度。是以有人想通過這個高度來判斷是否有NavigationBar 是不行的。當然4.0版本以下就不用說了。确認個問題,NavigationBar是4.0以上才有麼?
因為裝置有實體鍵仍然可以有一個導航欄。任何裝置運作自定義rom時都會設定一個選項,是否禁用的實體鍵,并添加一個導航欄。看看API:
ViewConfiguration.get(Context context).hasPermanentMenuKey() 有這麼一句描述 :Report if the device has a permanent menu key available to the user(報告如果裝置有一個永久的菜單主要提供給使用者).
android.view.KeyCharacterMap.deviceHasKey(int keyCode) 的描述:Queries the framework about whether any physical keys exist on the any keyboard attached to the device that are capable of producing the given key code(查詢架構是否存在任何實體鍵盤的任何鍵盤連接配接到裝置生産給出關鍵代碼的能力。).
那麼解決的辦法就是:
@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;
}
尊重原創、尊重作者,轉載請标明出處:
http://blog.csdn.net/lnb333666/article/details/41821149