Android : 搜索图标未显示在操作栏上
Android : Search Icon not showing on Action Bar
正如标题所说,我试图在我的操作栏中添加一个图标(用于搜索目的),但我得到的只是三点菜单中的项目标题。这是我使用的代码。
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>
使用以下代码即可:
<item
android:id="@+id/action_search"
android:icon="@drawable/search"
android:showAsAction="always"
android:title="action_search" />
在您的代码中您使用的是 app:showAsAction="always" 这是错误的用法 android:showAsAction="always"
正如标题所说,我试图在我的操作栏中添加一个图标(用于搜索目的),但我得到的只是三点菜单中的项目标题。这是我使用的代码。
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>
使用以下代码即可:
<item
android:id="@+id/action_search"
android:icon="@drawable/search"
android:showAsAction="always"
android:title="action_search" />
在您的代码中您使用的是 app:showAsAction="always" 这是错误的用法 android:showAsAction="always"