我最近将我的应用更新到appcompat到v7:22.2.0。我在操作栏中有一个搜索视图,在更新之前工作正常,但现在当我按下搜索图标时,搜索字段会显示在标题下方。
这是我的代码:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/search_participants"
android:icon="@drawable/search_white"
android:title="@string/search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="collapseActionView|always"/>
</menu>
它仍然有效,功能齐全,使用SearchView时仍然只显示操作栏标题。我怎样才能防止这种情况发生?我应该手动将标题设置为空字符串还是有一个不错的解决方案?
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
mOptionsMenu = menu;
if (mOptionsMenu != null) {
menu.clear();
mMenuInflater.inflate(R.menu.live_tracking_overview_menu, menu);
mSearchItem = menu.findItem(R.id.search_participants);
mSearchView = (SearchView) MenuItemCompat.getActionView(mSearchItem);
mSearchView.setOnQueryTextListener(this);
mSearchView.setQueryHint(getString(R.string.search));
MenuItemCompat.setOnActionExpandListener(mSearchItem, new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
showSearch();
//i could hide the title here, but that's an ugly fix
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
hideSearch();
return true;
}
});