检查Android设备是否具有软件或硬件导航按钮 [英] Check if Android device has software or hardware navigation buttons

查看:81
本文介绍了检查Android设备是否具有软件或硬件导航按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个带有透明导航栏的Android应用.要相应地调整布局,我需要知道设备是否具有软件或硬件按钮.

I'm writing an Android app with a transparent navigation bar. To adjust the layout accordingly, I need to know whether the device has software or hardware buttons.

在研究过程中,我遇到了以下解决方案:

During my research I came across the following solutions:

boolean hasMenuKey = ViewConfiguration.get(context).hasPermanentMenuKey();

这种方法不适用于某些没有菜单键但仍然具有硬件按钮的新设备,例如Samsung Note 4或HTC One.

This approach does not work on some new devices like the Samsung Note 4 or the HTC One which do not have a menu key, but still have hardware buttons.

boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
boolean hasHomeKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_HOME);
boolean hasHardwareButtons = hasBackKey && hasHomeKey;

这不太好,因为某些设备(如Sony Xperia Z3 Compact)虽然具有软件导航栏,但返回true.

This does not work as well, because some devices like the Sony Xperia Z3 Compact return true although it has a software navigation bar.

有没有可靠的方法来找出是否存在物理按钮或软件导航栏?

Is there any reliable way to find out whether there are physical buttons or a software navigation bar?

推荐答案

请参见此答案.您可以获取显示器的实际高度(以像素为单位),然后获取应用程序可用的高度,并确定设备是否具有带有这些值的屏幕导航栏.

See this answer. You can get the real height of the display in pixels, and then get the height available to your app, and figure out if the device has an on-screen navigation bar with these values.

这篇关于检查Android设备是否具有软件或硬件导航按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆