如何以编程方式确定 Android 设备是否有菜单按钮?

发布于 2024-10-27 07:10:14 字数 339 浏览 6 评论 0原文

我需要弥补 Android 3 平板电脑上缺少物理(甚至“软”)菜单按钮的问题。我正在开发的应用程序通常会隐藏其活动的标题栏,但如果我希望操作栏出现在平板电脑上,我就无法这样做。 API 中是否有某些内容可以用来确定设备是否有菜单按钮?

如果我找不到这样的功能,我唯一能想到的就是在Android 3.0及更高版本上永远不要隐藏标题栏,但这让我烦恼有两个原因:

  1. 我相信Android 3.0(或更高版本的API)最终将支持手机,手机可能会有菜单按钮
  2. 我不希望对 SDK 版本进行硬编码(也许我永远无法隐藏 Android 3.0 及更高版本的标题栏,但我会冒着不在手机上隐藏标题栏的风险)

I need to compensate for the lack of a physical (or even a "soft") menu button on Android 3 tablets. The app I'm working on generally hides the title bar for its activities, but I can't do that if I want the action bar to appear on a tablet. Is there something in the API that I can use to determine whether a device has a menu button?

If I can't find such a function, the only thing I can think of to do is to never hide the title bar on Android 3.0 and later, but that bothers me for two reasons:

  1. I believe Android 3.0 (or a later API) will eventually support phones, which will probably have menu buttons
  2. I would prefer not to hard code that SDK version (perhaps I could never hide the title bar for Android 3.0 and above, but then I run the risk of not hiding the title bar on phones)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无需解释 2024-11-03 07:10:14

LeffelMania 的想法是正确的,但有一个警告:如果您以 SDK 11 (3.0) 为目标并使用无标题栏主题或使用某种自定义标题覆盖,您将只是简单的无菜单,因为目标设置为 11 的蜂窝设备的选项菜单位于标题栏本身,而不是底部状态栏中的虚假菜单按钮。所以那里要小心! (在这种情况下,您可以通过设置较低的目标或将样式资源定位到 v11 来解决此问题,以使用带有标题栏的 Holo 主题之一)。

LeffelMania has the right idea, but there is a caveat: If you target SDK 11 (3.0) and use a no-title-bar theme or override with a custom title of some sort, you will be just plain menu-less, because on Honeycomb devices with the target set to 11 the options-menu lives in the title bar itself instead of a fake menu button in the bottom status bar. So be careful there! (You can work around this by setting a lower target, or targeting style resources to v11 to use a one of the Holo themes with a titlebar in that case).

一场信仰旅途 2024-11-03 07:10:14

已弃用:鉴于 Android 框架和设计指南的更改,此答案不再准确。


Android 手机保证有菜单按钮。 Honeycomb 之前的所有手机都有菜单按钮,并且 Honeycomb 具有始终存在的软件按钮。如果您使用与菜单按钮相关的正确回调(onCreateOptionsMenu()、onOptionsItemSelected() 等),您将可以在任何运行 Android 的设备上安全地接收这些回调。

DEPRECATED: This answer is no longer accurate, given changes to the Android framework and design guidelines.


Android phones are guaranteed to have a Menu button. All phones pre-Honeycomb have Menu buttons, and Honeycomb features an ever-present software button. If you use the proper callbacks that tie into the Menu button (onCreateOptionsMenu(), onOptionsItemSelected(), etc) you will be safe to receive those callbacks on any device running Android.

调妓 2024-11-03 07:10:14

As of API level 14, ViewConfiguration.hasPermanentMenuKey() is available to indicate whether a permanent menu key is present.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文