我在菜单文件夹中使用搜索项创建了一个xml文件。
<item android:id="@+id/action_search"
android:title="@string/search_hint"
android:icon="@drawable/ic_magnify_white_24dp"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView"
/>
我还使用搜索功能
设置菜单@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_option, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) this.getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
//start search activity here
onSearchRequested();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
问题在于,当我调用onSearchRequested()时,搜索小部件不会与工具栏集成。看图像:这就是它应该是什么样的(当我没有调用onSearchRequested()时。我认为文档说,需要调用onSearchRequested?):搜索小部件有时也加载两次。
非常感谢任何帮助。谢谢!