[英]Items not showing in the ActionBar with showAsAction=“always”
I'm new to Android programming and currently trying to get the Actionbar working. 我是Android编程的新手,目前正试图让Actionbar正常工作。 My problem is, that although I set an item in the XML file to "always", it constantly ends up in the overflow menu no matter what I try. 我的问题是,虽然我将XML文件中的项目设置为“始终”,但无论我尝试什么,它都会不断地出现在溢出菜单中。 I found several similar problems on here, but none of the solutions fixed the problem. 我在这里发现了几个类似的问题,但没有一个解决方案解决了这个问题。
Additional info: The ic_action_search icons are in the drawable folders of the project. 附加信息:ic_action_search图标位于项目的可绘制文件夹中。
main_activity_actions.xml: main_activity_actions.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">
<!-- Search, always displayed -->
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
app:showAsAction="always" />
<!-- Settings, should always be in the overflow -->
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
app:showAsAction="never" />
</menu>
strings.xml: strings.xml中:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">My First App</string>
<string name="edit_message">Enter a message</string>
<string name="button_send">Send</string>
<string name="action_settings">Settings</string>
<string name="action_search">Search</string>
<string name="title_activity_main">MainActivity</string>
<string name="title_activity_display_message">My Message</string>
<string name="hello_world">Hello world!</string>
</resources>
In MainActivity.java 在MainActivity.java中
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
Thanks for your help! 谢谢你的帮助!
try android:showAsAction
instead of app:showAsAction
. 尝试android:showAsAction
而不是app:showAsAction
。 Or if you're using the appcompat_v7
backport, use both android:showAsAction
and app:showAsAction
(Thanks to Commonsware). 或者,如果您正在使用appcompat_v7
backport,请同时使用android:showAsAction
和app:showAsAction
(感谢Commonsware)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.