Android appcompat 操作栏菜单项 showAsAction 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21871750/
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
Android appcompat actionbar menu item showAsAction not working
提问by pickle_inspector
I have a menu item that is showing up on android 4.x but not on 2.x. Here is my menu.xml
我有一个菜单项显示在 android 4.x 上,但不在 2.x 上。这是我的 menu.xml
<?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/menu_filter"
android:title="Filter"
app:showAsAction="always"/>
</menu>
This is my actionbar style
这是我的操作栏样式
<style name="style1_actionBar" parent="@style/Widget.AppCompat.Light.ActionBar">
<item name="android:background">@color/blue_dark</item>
<item name="android:textColor">@color/white</item>
<item name="actionMenuTextAppearance">@color/white</item>
<item name="background">@color/blue_dark</item>
</style>
Any ideas?
有任何想法吗?
Edit: removed double quote typo
编辑:删除双引号错字
Could it be the fact that I am showing only text, no icons? I'm kind of stuck here.
难道是因为我只显示文本,没有图标?我有点卡在这里。
回答by pickle_inspector
Whew, thanks for your help guys but I managed to figure it out. It wasn't a problem with the xml, it was a problem with the onCreateOptionsMenu function.
哇,谢谢你们的帮助,但我设法弄明白了。这不是 xml 的问题,而是 onCreateOptionsMenu 函数的问题。
I was using this
我正在使用这个
new MenuInflater(getApplication()).inflate(R.menu.activity_wentry_editor, menu);
instead of this
而不是这个
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_wentry_editor, menu);
Not entirely sure why this works but it does.
不完全确定为什么会这样,但确实如此。
回答by Ramesh
<menu xmlns:android="http://schemas.android.com/apk/res/android"
**xmlns:yourapp="http://schemas.android.com/apk/res-auto"** >
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
**yourapp**:showAsAction="ifRoom" />
</menu>
Please refer to the documentation. http://developer.android.com/guide/topics/ui/actionbar.html
请参阅文档。http://developer.android.com/guide/topics/ui/actionbar.html
Using XML attributes from the support library
使用支持库中的 XML 属性
Notice that the showAsAction attribute above uses a custom namespace defined in the tag. This is necessary when using any XML attributes defined by the support library, because these attributes do not exist in the Android framework on older devices. So you must use your own namespace as a prefix for all attributes defined by the support library.
请注意,上面的 showAsAction 属性使用标记中定义的自定义命名空间。当使用支持库定义的任何 XML 属性时,这是必要的,因为这些属性在旧设备上的 Android 框架中不存在。因此,您必须使用自己的命名空间作为支持库定义的所有属性的前缀。
回答by Mario Eraso
In my case I had to add a few lines to onCreateOptionsMenu.
就我而言,我不得不向 onCreateOptionsMenu 添加几行。
Android Studio didn't let me use android:showAsAction="ifRoom" while using appCompat.
Android Studio 不允许我在使用 appCompat 时使用 android:showAsAction="ifRoom"。
app:showAsAction="ifRoom" wasn't working and I removed it without problems.
app:showAsAction="ifRoom" 不起作用,我毫无问题地将其删除。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
for (int i = 0; i < menu.size(); i++) {
menu.getItem(i).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
return super.onCreateOptionsMenu(menu);
}
回答by Sandro Machado
Using the menu in an activity that extends the AppCompact it is necessary import the app context in the XML and use it:
使用扩展 AppCompact 的活动中的菜单,有必要在 XML 中导入应用程序上下文并使用它:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- "Mark Favorite", should appear as action button if possible -->
<item
android:id="@+id/action_favorite"
android:icon="@drawable/ic_favorite_black_48dp"
android:title="@string/action_favorite"
app:showAsAction="ifRoom"/>
<!-- Settings, should always be in the overflow -->
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
app:showAsAction="never"/>
</menu>
What you need to do basically is add xmlns:app="http://schemas.android.com/apk/res-auto"
to the menu element in yout XML and use the showAsActionin the following format: app:showAsAction="ifRoom"
.
您需要做的基本上是添加 xmlns:app="http://schemas.android.com/apk/res-auto"
到 XML 中的菜单元素并使用以下格式的showAsAction:app:showAsAction="ifRoom"
.
This will show the icon in the action bar, if possible.
如果可能,这将在操作栏中显示图标。