我已经实现了应用内搜索功能。当我保持方向固定时,它可以很好地退出。但是,如果在输入我的查询后我改变方向折叠搜索视图然后我得到溢出图标代替我的搜索图标,当更改方向时再次修复该搜索图标。此外,溢出图标在单击时不显示搜索选项。
changing orientation after entering query.
这是我的菜单xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/search"
android:title="@string/search_title"
android:icon="@drawable/ic_search_white_24dp"
android:orderInCategory="1"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
这是我的片段onCreateOptionsMenu
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
menu.clear();
inflater.inflate(R.menu.options_menu, menu);
searchString = getActivity().getPreferences(0).getString("searchString", null);
final MenuItem searchMenuItem = menu.findItem(R.id.search);
searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
searchView.setIconifiedByDefault(true);
searchView.setQueryHint(getResources().getString(R.string.search_hint));
//set listener on search view to fire searchResultHandlerFrag
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
Fragment fragment = new SearchHandlerFrag();
Bundle bundle = new Bundle();
bundle.putString("query", query);
fragment.setArguments(bundle);
searchView.clearFocus();
getActivity().getSupportFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.mainContainer, fragment).commit();
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
//repopulate search view with existing search string, if any
if (!TextUtils.isEmpty(searchString)) {
if(!searchMenuItem.isActionViewExpanded())
searchMenuItem.expandActionView();
searchView.setQuery(searchString, false);
searchView.clearFocus();
}
//trying to fix the overflow problem
MenuItemCompat.setOnActionExpandListener(searchMenuItem, new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
searchView.setIconified(true);
return true;
}
});
}
任何帮助将不胜感激。提前谢谢。
答案 0 :(得分:2)
好的,回答我自己的问题感觉很棒。诀窍是将app:showAsAction="collapseActionView|ifRoom"
更改为app:showAsAction="collapseActionView|always"
。感谢Signo。
答案 1 :(得分:1)
试试这个,在你的xml定义中改变:
app:showAsAction="collapseActionView|ifRoom"
使用:
app:showAsAction="always"
应该始终显示搜索图标,而不是显示collapseActionView
图标。