如何在 Android ActionBar 兼容上强制溢出菜单?
Android 操作栏兼容性
是否可以?在较旧的设备(3.0 之前)上,仅当按下菜单键时才会显示不适合操作栏的项目,我希望将这些项目分组在操作栏的溢出菜单中。
Android action bar compat
Is it possible? On older devices (pre 3.0) the items that don't fit the action bar are only shown when the menu key is pressed, I want these items to be grouped in the actionbar's overflow menu.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
仅当设备上没有可用的硬菜单按钮时,操作溢出菜单才可用。我在“用户界面”>“框架主题”下发现了这一点。操作栏,请查看此处的第三个项目符号。
有一个由 Jake Wharton 编写的操作栏库,名为 ActionBarSherlock。也许即使在较旧的设备(包括硬菜单按钮)上,这也能为您提供操作溢出菜单样式,但我还没有研究过这一点。
编辑: ActionBarSherlock 4.0(当前是候选版本)内置了强制操作溢出的功能。如果您想自己扩展 ActionBarCompat 示例,您可以查看 github 以了解 Jake 是如何实现它的。我建议一起考虑使用他的库,因为它做得非常好。
如果您选择使用 Jake 的库,请考虑将 Activity 主题设置为 @style/Theme.Sherlock.ForceOverflow 以在旧设备上强制显示溢出菜单。
编辑2:使用
ForceOverflow
主题会导致问题(示例#1)在带有硬件菜单按钮的设备上。因此,Jake Wharton 将在未来版本中删除ForceOverflow
。The action overflow menu is only available when there is no hard menu button available on the device. I found this stated in the Framework Topics under User Interface > Action Bar, check out the 3rd bullet here.
There is an action bar library written by Jake Wharton called ActionBarSherlock. Perhaps this is able to supply you with an action overflow menu style even when on older devices (which include a hard menu button), however I have not looked into this.
Edit: ActionBarSherlock 4.0 (currently a release candidate) has functionality built in to force action overflow. If you want to extend the ActionBarCompat example yourself, you could take a look on github to get an idea how Jake implemented it. I would suggest just looking into using his library all together, as it is very well done.
If you choose to use Jake's library, look into setting up the Activity theme as @style/Theme.Sherlock.ForceOverflow to force the overflow menu on older devices.
Edit2: Using
ForceOverflow
theme causes issues (example #1) on devices with hardware menu button. Thus, Jake Wharton is going to removeForceOverflow
in the future versions.好吧,这很简单,但很难弄清楚。
您首先需要一个要用作溢出充气器的菜单项。示例
获得项目后,添加一个子菜单,其中包含您想要在溢出菜单中添加的项目。示例:
单击后,其中的其他项目将会膨胀。我的应用程序使用的是 ActionBarSherlock 4.0,因此在这对您有用之前,您需要访问“SplitActionBar”。 (仍然适用于默认的 Android Actionbar)具体操作
方法如下:
在 AndroidManifest.xml 文件中,您需要在需要溢出菜单的 Activity 下添加此代码。
老实说,操作栏是否分开并不重要,但我更喜欢它。
注意: 使溢出菜单膨胀的项目必须
showAsAction="always"
Vwola!你有一个溢出菜单!希望我能帮到你。 :)
Okay, this is simple but hard to figure out.
You first need a menu item you want to use as the overflow inflater. Example
Once you have your item, add a sub-menu containing your items you want in the overflow menu. Example:
On click this will inflate other items within. My application is using ActionBarSherlock 4.0 so before this will work for you, you will need to access the "SplitActionBar". (Will still work on default android Actionbar)
Here's how:
In your AndroidManifest.xml file, you need to add this code under the activity you need the overflow menu in.
Honestly it shouldn't matter if you have the actionbar split or not but I prefer it.
NOTE: Your item that inflates your overflow menu MUST
showAsAction="always"
Vwola! you have an overflow menu! Hope I helped you out. :)
按照 LeviRockerSk8er 的建议,我强制在操作栏中设置一个溢出菜单,如下所示:
这是“menu.xml”的代码:
Following LeviRockerSk8er's suggestion I've forced to have a overflow menu in the action bar like this:
This is the code for "menu.xml":