Android 如何强制显示操作栏溢出图标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20444596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to force action bar overflow icon to show
提问by Peri Hartman
I would like to force the overflow icon to always show in the action bar (assuming there are overflow items). On models with a menu button, sometimes the overflow icon doesn't appear and users must tap the devices menu button to get the rest of the action menu items. Users keep complaining about this.
我想强制溢出图标始终显示在操作栏中(假设有溢出项)。在带有菜单按钮的型号上,有时不会出现溢出图标,用户必须点击设备菜单按钮才能获得其余的操作菜单项。用户不断抱怨这个。
Note that for the context menu, the overflow icon always shows, regardless of whether the device has a built in menu button or not.
请注意,对于上下文菜单,无论设备是否具有内置菜单按钮,始终显示溢出图标。
I realize that forcing the overflow icon to appear in the action bar would duplicate the functionality of the "physical" one. You might consider that violating Androids design guidelines. In my opinion, though, the users win. They say it's confusing and I believe they're right.
我意识到强制溢出图标出现在操作栏中会复制“物理”图标的功能。您可能会认为这违反了 Android 设计指南。不过,在我看来,用户赢了。他们说这令人困惑,我相信他们是对的。
回答by CommonsWare
Congratulations! You won!
恭喜!你赢了!
As of Android 4.4, the ... affordance in the action bar will be there, regardless of whether the device has a physical MENU button or not. Google's current Compatibility Definition Documentnow comes out a bit more forcefully against having a dedicated MENU button.
从 Android 4.4 开始,操作栏中的...可供性将存在,无论设备是否具有物理 MENU 按钮。谷歌当前的兼容性定义文档现在更加强烈地反对拥有专用的 MENU 按钮。
The hack that developers have used in the past, to get this behavior, is:
开发人员过去使用的 hack 来获得这种行为,是:
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if (menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
}
catch (Exception e) {
// presumably, not relevant
}
That should not be needed on Android 4.4+, though with the exception handler in place, I would not expect any particular problem if you run it and, someday, they get rid of sHasPermanentMenuKey
outright.
这在 Android 4.4+ 上应该不需要,尽管有了异常处理程序,如果你运行它,我不希望有任何特别的问题,总有一天,它们会sHasPermanentMenuKey
彻底摆脱。
Personally, I still wouldn't change things on Android 4.3 and below, as I suspect it's a whack-a-mole situation, where you will replace complaints about having no menu with complaints about having duplicate versions of the same menu. That being said, since this is now the officially endorsed behavior going forward, I have no problems with developers aiming for consistency on older devices.
就我个人而言,我仍然不会在 Android 4.3 及以下版本上进行更改,因为我怀疑这是一种打地鼠的情况,在这种情况下,您会将有关没有菜单的抱怨替换为对同一菜单有重复版本的抱怨。话虽如此,由于这是现在官方认可的行为,我对旨在在旧设备上保持一致性的开发人员没有任何问题。
A hat tip to the commenter on the issue I filedregarding this, pointing out the change.
回答by Rino
This could be another work around, which really helped me. Keep one drawable with three dots and give it as a menu item.
这可能是另一种解决方法,这确实对我有帮助。保持一个带有三个点的可绘制对象并将其作为菜单项。
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/saveDetails"
app:showAsAction="always"
android:title="@string/save"/>
<item
android:id="@+id/menu_overflow"
android:icon="@drawable/dts"
android:orderInCategory="11111"
app:showAsAction="always">
<menu>
<item
android:id="@+id/contacts"
app:showAsAction="never"
android:title="Contacts"/>
<item
android:id="@+id/service_Tasks"
app:showAsAction="never"
android:title="Service Tasks"/>
<item
android:id="@+id/charge_summary"
app:showAsAction="never"
android:title="Charge Summary"/>
<item
android:id="@+id/capture_signature"
app:showAsAction="never"
android:title="Capture Signature"/>
</menu>
</item>
</menu>