我是Android编程的新手,目前正试图让Actionbar正常工作.我的问题是,虽然我将XML文件中的项目设置为“始终”,但无论我尝试什么,它都会不断地出现在溢出菜单中.我在这里发现了几个类似的问题,但没有一个解决方案解决了这个问题.
附加信息:ic_action_search图标位于项目的可绘制文件夹中.
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中:
<?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>
在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);
}
谢谢你的帮助!
解决方法:
尝试android:showAsAction而不是app:showAsAction.或者,如果您正在使用appcompat_v7 backport,请同时使用android:showAsAction和app:showAsAction(感谢Commonsware).