如何在带有菜单按钮的设备上强制使用溢出菜单
我希望所有不适合操作栏的菜单项都进入溢出菜单(从操作栏而不是菜单按钮到达的菜单项)即使在这样做的设备上有一个菜单按钮。对于用户来说,这似乎比将它们放入单独的菜单列表中更直观,后者要求用户从触摸(屏幕)交互跳转到基于按钮的交互,仅仅是因为 ActionBar 的布局无法将它们放在栏上。
在模拟器上,我可以将“硬件后退/主页键”值设置为“否”并获得此效果。 我已经在寻找一种方法来在代码中为具有菜单按钮但无法精细的实际设备执行此操作。有人可以帮助我吗?
I'd like to have all of the menu items that don't fit into the ActionBar go into the overflow menu (the one that is reached from the Action Bar not the menu button) even on devices that do have a Menu button. This seems much more intuitive for users than throwing them into a separate menu list that requires the user to jump from a touch(screen) interaction to a button based interaction simply because the layout of the ActionBar can't fit them on the bar.
On the emulator I can set the "Hardware Back/Home Keys" value to "no" and get this effect.
I've searched for a way to do this in code for an actual device that has a menu button but can't fine one. Can anyone help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
您还可以在这里使用这个小技巧:
放置它的好地方是应用程序类的
onCreate
方法。它将强制应用程序显示溢出菜单。菜单按钮仍然有效,但会打开右上角的菜单。
[编辑] 因为它已经出现了好几次了:这个 hack 仅适用于 Android 3.0 中引入的本机 ActionBar,不适用于 ActionBarSherlock。后者使用自己的内部逻辑来决定是否显示溢出菜单。如果使用ABS,所有平台< 4.0 由 ABS 处理,因此受制于其逻辑。该黑客仍然适用于所有 Android 4.0 或更高版本的设备(您可以安全地忽略 Android 3.x,因为实际上没有任何带有菜单按钮的平板电脑)。
存在一个特殊的 ForceOverflow-Theme,它将强制 ABS 中的菜单,但显然它将在未来版本中删除,因为并发症。
You can also use this little hack here:
Good place to put it would be the
onCreate
-Method of your Application class.It will force the App to show the overflow menu. The menu button will still work, but it will open the menu in the top right corner.
[Edit] Since it has come up several times now: This hack only works for the native ActionBar introduced in Android 3.0, not ActionBarSherlock. The latter uses its own internal logic to decide whether to show the overflow menu. If you use ABS, all platforms < 4.0 are handled by ABS and are thus subjected to its logic. The hack will still work for all devices with Android 4.0 or greater (you can safely ignore Android 3.x, since there aren't really any tablets out there with a menu button).
There exists a special ForceOverflow-Theme that will force the menu in ABS, but apperently it is going to be removed in future versions due to complications.
编辑:修改以回答物理菜单按钮的情况。
这实际上是通过设计来防止的。根据 Android 设计指南的兼容性部分,
“...操作溢出可从菜单硬件键。弹出的操作结果...显示在屏幕底部。”
您会在屏幕截图中注意到,带有物理菜单按钮的手机在 ActionBar 中没有溢出菜单。这避免了用户的歧义,本质上有两个按钮可用于打开完全相同的菜单。
解决跨设备的一致性问题:最终,对于用户体验而言,您的应用程序与同一设备上的所有其他应用程序的行为一致比它在所有设备上与自身的行为一致更重要。
EDIT: Modified to answer for the situation of physical menu button.
This is actually prevented by design. According to the Compatibility Section of the Android Design Guide,
"...the action overflow is available from the menu hardware key. The resulting actions popup... is displayed at the bottom of the screen."
You'll note in the screenshots, phones with a physical menu button don't have an overflow menu in the ActionBar. This avoids ambiguity for the user, essentially having two buttons available to open the exact same menu.
To address the issue of consistency across devices: Ultimately it's more important to the user experience that your app behave consistently with every other app on the same device, than that it behave consistently with itself across all devices.
我通常通过像这样定义我的菜单来解决这个问题(也在我的示例中使用了 ActionBarSherlock 图标):
我承认这可能需要在 xml 中手动“溢出管理”,但我发现这个解决方案很有用。
您还可以在您的活动中强制设备使用硬件按钮打开溢出菜单:
:-)
I use to workaround it by defining my menu like this (also with ActionBarSherlock icon used in my example):
I admit that this may require manual "overflow-management" in your xml, but I found this solution useful.
You can also force device to use HW button to open the overflow menu, in your activity:
:-)
如果您使用支持库 (
android.support.v7.app.ActionBar
) 中的操作栏,请使用以下命令:If you are using the action bar from the support library (
android.support.v7.app.ActionBar
), use the following:这种方法被 Android 开发者设计系统阻止,但我找到了一种方法来传递它:
将其添加到 XML 菜单文件中:
接下来,创建一个名为“AppPickActionProvider”的类,并将以下代码复制到其中:
This kind of method is prevented by the Android Developers Design System, but I found a way to pass it:
Add this to your XML menu file:
Next, create a class named 'AppPickActionProvider', and copy the following code to it:
好吧,我认为亚历山大·卢卡斯(Alexander Lucas)提供了(不幸的是)正确的答案,所以我将其标记为“正确”的答案。我在这里添加的替代答案只是将任何新读者指向 此帖子位于Android 开发者博客对该主题进行了相当完整的讨论,并提供了一些关于从 11 级之前过渡到新操作栏时如何处理代码的具体建议。
我仍然认为这是一个设计错误,没有让菜单按钮在启用菜单按钮的设备中充当冗余的“操作溢出”按钮,作为过渡用户体验的更好方式,但此时它已经过时了。
Well I think that Alexander Lucas has provided the (unfortunately) correct answer so I'm marking it as the "correct" one. The alternative answer I'm adding here is simply to point any new readers to this post in the Android Developers blog as a rather complete discussion of the topic with some specific suggestions as to how to deal with your code when transitioning from pre-level 11 to the new Action Bar.
I still believe it was a design mistake not have the menu button behave as a redundant "Action Overflow" button in menu button enabled devices as a better way to transition the user experience but its water under the bridge at this point.
我不确定这是否是您正在寻找的,但我在 ActionBar 的菜单中构建了一个子菜单,并将其图标设置为与溢出菜单的图标相匹配。尽管它不会自动发送项目(即您必须选择始终可见的内容和始终溢出的内容),但在我看来,这种方法可能会对您有所帮助。
I'm not sure if this is what you're looking for, but I built a Submenu within the ActionBar's Menu and set its icon to match the Overflow Menu's Icon. Although it wont have items automatically sent to it, (IE you have to choose what's always visible and what's always overflowed) it seems to me that this approach may help you.
在预装 ICS 的 gmail 应用程序中,当您选择了多个项目时,菜单按钮将被禁用。此处,溢出菜单是通过使用溢出按钮而不是物理菜单按钮“强制”触发的。有一个名为 ActionBarSherlock 的第 3 方库,它可以让您“强制”溢出菜单。但这仅适用于 API 级别 14 或更低(ICS 之前)
In the gmail app that comes with ICS pre-installed, the menu button is disabled when you have multiple items selected. The overflow menu is here "forced" to be triggered by the use of the overflow button instead of the physical menu button. Theres a 3rd-party lib called ActionBarSherlock which lets you "force" the overflow menu. But this will only work on API level 14 or lower(pre-ICS)
如果您使用 工具栏,您可以在所有版本和所有设备,我在一些 2.x 设备上尝试过,它可以工作。
If you use Toolbar, you can show the overflow on all versions and all devices, I've tried on some 2.x devices, it works.
抱歉,如果这个问题已经解决了。
这是我为解决该错误所做的操作。我转到布局并创建了两个包含工具栏的布局。一个是 sdk 版本 8 的布局,另一个是 sdk 版本 21 的布局。在版本 8 上,我使用 android.support.v7.widget.Toolbar,而在 sdk 21 布局上使用 android.widget.Toolbar。
然后我在活动中膨胀工具栏。我检查 sdk 是否为 21 或更高。然后我膨胀相应的布局。这会强制硬件按钮映射到您实际设计的工具栏上。
Sorry if this problem is dead.
Here is what I did to resolve the error. I went to layouts and created two ones containing toolbars. One was a layout for sdk version 8 and the other was for sdk version 21. On version 8, I used the android.support.v7.widget.Toolbar while I used android.widget.Toolbar on the sdk 21 layout.
Then I inflate the toolbar in my activity. I check the sdk to see if it was 21 or higher. I then inflate the corresponding layout. This forces the hardware button to map onto the toolbar you actually designed.
对于使用新
工具栏
的任何人:For anyone using the new
Toolbar
: