是否有可能必须使用appcompat库操作栏菜单?有可能、菜单、操作、appcompat

2023-09-12 21:54:51 作者:爱你爱到心痛乄゛我心好累

最近我从常规操作栏执行切换到最近发布的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:应用程序属性菜单标签,就像这样:

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.