是否可以使用 appcompat 库创建操作栏菜单? [英] Is it possible to have a action bar menu using the appcompat library?

查看:19
本文介绍了是否可以使用 appcompat 库创建操作栏菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我从常规操作栏实现切换到最近发布的 appcompat 实现.我的应用大量使用操作栏来提供功能.自切换以来,在较旧的地点API(少于 11 个)没有任何菜单项.较新的 API 可以,但它们不会像配置的那样显示图像(如果房间|withText).有没有其他人遇到过这种情况或提出过任何解决方案?

recently I've switched from the regular action bar implementation to the recently released appcompat implementation. My app made heavy use of the action bar to provide functionality. Since switching, on older spots APIs (less than 11) don't have any menu items. And newer APIs do, but they don't show the image like configured (if room|withText). Has anyone else experienced this or came up with any solutions?

推荐答案

我在使用 appcompat 库时发现了问题所在.您可以像往常一样创建菜单.

I found out what was up, when using the appcompat library. You can create your menu just like normal.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
   return true;
}

但是,在您的菜单 xml 文件中,将 xmlns:app 属性添加到菜单标记,如下所示:

But, in your menu xml files, add a xmlns:app attribute to the menu tag, like so:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >

然后,在您通常指定showAs"样式(ifRoom、withText 等)的每个菜单项中,在常规菜单项旁边包含此替代行:

then, in each of your menu items, where you usually specify the "showAs" style (ifRoom, withText, etc.), include this alternative line alongside the regular one:

app:showAsAction="ifRoom|withText"
android:showAsAction="ifRoom|withText"

此后,您的菜单将在当前和旧 API 上正确显示.我从这里获得了这些信息.

After this, your menus will show correctly on both current and old APIs. I got this information from here.

这篇关于是否可以使用 appcompat 库创建操作栏菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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