欢迎访问 生活随笔!

凯发k8官方网

当前位置: 凯发k8官方网 > 运维知识 > android >内容正文

android

android 检查设备是否存在 导航栏 navigationbar -凯发k8官方网

发布时间:2025/1/21 android 34 豆豆
凯发k8官方网 收集整理的这篇文章主要介绍了 android 检查设备是否存在 导航栏 navigationbar 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

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 barboolean hasmenukey = viewconfiguration.get(activity).haspermanentmenukey();boolean hasbackkey = keycharactermap.devicehaskey(keyevent.keycode_back);if (!hasmenukey && !hasbackkey) {// 做任何你需要做的,这个设备有一个导航栏return true;}return false;}

 

总结

以上是凯发k8官方网为你收集整理的android 检查设备是否存在 导航栏 navigationbar的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得凯发k8官方网网站内容还不错,欢迎将凯发k8官方网推荐给好友。

网站地图