正如标题所示我试图在我的动作栏中添加一个图标(用于搜索目的),但我得到的只是三点菜单中的项目标题.这是我使用的代码.
menu_main.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="@+id/action_search"
android:icon="@drawable/search"
android:orderInCategory="100"
android:title="action_search"
app:showAsAction="always"/>
</menu>
我给菜单充气的方式:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_search) {
return true;
}
return super.onOptionsItemSelected(item);
}
你们知道为什么会这样吗?
PS:我有holo.light作为我的应用程序的默认主题.
最佳答案 您正在为本机操作栏使用某些内容(例如,继承自FragmentActivity)以及appcompat-v7操作栏后台的一些内容(例如,app:showAsAction).
将menu_main.xml更改为:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="@+id/action_search"
android:icon="@drawable/search"
android:orderInCategory="100"
android:showAsAction="always"
android:title="action_search" />
</menu>