zoukankan      html  css  js  c++  java
  • Android 检查设备是否存在 导航栏 NavigationBar

    尊重原创、尊重作者,转载请标明出处:

    http://blog.csdn.net/lnb333666/article/details/41821149

    目前也没有可靠的方法来检查设备上是否有导航栏。可以使用KeyCharacterMap.deviceHasKey来检查设备上是否有某些物理键,比如说菜单键、返回键、Home键。然后我们可以通过存在物理键与否来判断是否有NavigationBar(一般来说手机上物理键、NavigationBar共存).

    1. public static int getNavigationBarHeight(Activity activity) {  
    2.         Resources resources = activity.getResources();  
    3.         int resourceId = resources.getIdentifier("navigation_bar_height",  
    4.                 "dimen", "android");  
    5.         //获取NavigationBar的高度  
    6.         int height = resources.getDimensionPixelSize(resourceId);  
    7.         return height;  
    8.     }  


    上面这段代码,在绝大多数情况下都能获取到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(查询框架是否存在任何物理键盘的任何键盘连接到设备生产给出关键代码的能力。).

    那么解决的办法就是:

      1. @SuppressLint("NewApi")   
      2.     public static boolean checkDeviceHasNavigationBar(Context activity) {  
      3.   
      4.         //通过判断设备是否有返回键、菜单键(不是虚拟键,是手机屏幕外的按键)来确定是否有navigation bar  
      5.         boolean hasMenuKey = ViewConfiguration.get(activity)  
      6.                 .hasPermanentMenuKey();  
      7.         boolean hasBackKey = KeyCharacterMap  
      8.                 .deviceHasKey(KeyEvent.KEYCODE_BACK);  
      9.   
      10.         if (!hasMenuKey && !hasBackKey) {  
      11.             // 做任何你需要做的,这个设备有一个导航栏  
      12.             return true;  
      13.         }  
      14.         return false;  
      15.     }  
  • 相关阅读:
    一个word小技巧
    Android 自定义组件之 带有悬浮header的listview
    传智播客 java基础 相关资料 Day2
    js取整 摘抄
    ifram子页面父页面相互调用
    寻知图项目收获--项目管理方面
    圣经学习 读经群读经记录(一)申命记5-7章
    java1234教程系列笔记 S1 Java SE chapter 02 写乘法口诀表
    java1234教程系列笔记 S1 Java SE chapter 02 lesson 03 java基本数据类型
    java1234教程系列笔记 S1 Java SE 02 eclipse初步使用、注释、标识符
  • 原文地址:https://www.cnblogs.com/sage-blog/p/4153241.html
Copyright © 2011-2022 走看看