本文介绍了展开搜索视图以使用整个操作栏(隐藏其他内容)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的ActionBar
内部有一个SearchView
,并且我想在按下搜索图标时使用整个ActionBar
,但是我只能使用ActionBar
可用空间
I have a SearchView
inside my ActionBar
, and I want to use the entire ActionBar
when the search icon is pressed, but I can only use the ActionBar
free space
我的菜单代码:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="br.com.moderna.houaiss.activity.SearchActivity" >
<item
android:id="@+id/search"
android:actionViewClass="android.widget.SearchView"
android:icon="@drawable/ic_search_white_48dp"
android:showAsAction="collapseActionView|always"
android:title="@string/search"/>
<item
android:id="@+id/backWardHistory"
android:icon="@drawable/ic_arrow_back_white_48dp"
android:showAsAction="always"
android:title="@string/back_history"/>
<item
android:id="@+id/forWardHistory"
android:icon="@drawable/ic_arrow_forward_white_48dp"
android:showAsAction="always"
android:title="@string/forward_history"/>
<item
android:id="@+id/action_home"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/action_home"/>
<item
android:id="@+id/action_about"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/action_about"/>
<item
android:id="@+id/action_configuration"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/action_configuration"/>
<item
android:id="@+id/action_logout"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/action_logout"/>
我尝试过android:showAsAction="ifRoom"
,但是我需要它们始终位于我的ActionBar
上,而不要放在菜单内.
I had tryed android:showAsAction="ifRoom"
, but I need them to be always on my ActionBar
, and not inside my menu.
编辑------
感谢@Simas,
我使用setOnActionExpandListener方法来实现此效果
I used setOnActionExpandListener method to achieve this effect
final MenuItem searchItem = menu.findItem(R.id.search);
searchItem.setOnActionExpandListener(new OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(final MenuItem item) {
SearchActivity.this.setItemsVisibility(menu, searchItem, false);
return true;
}
@Override
public boolean onMenuItemActionCollapse(final MenuItem item) {
SearchActivity.this.setItemsVisibility(menu, searchItem, true);
return true;
}
});
private void setItemsVisibility(final Menu menu, final MenuItem exception,
final boolean visible) {
for (int i = 0; i < menu.size(); ++i) {
MenuItem item = menu.getItem(i);
if (item != exception)
item.setVisible(visible);
}
}
推荐答案
好吧,当SearchView
展开时,您可以通过隐藏所有其他项来模仿自己:
Well you could imitate that yourself by hiding all the other items when the SearchView
is expanded:
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
final MenuItem searchItem = menu.findItem(R.id.search);
SearchView searchView = (android.widget.SearchView) searchItem.getActionView();
// Detect SearchView icon clicks
searchView.setOnSearchClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setItemsVisibility(menu, searchItem, false);
}
});
// Detect SearchView close
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
setItemsVisibility(menu, searchItem, true);
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
private void setItemsVisibility(Menu menu, MenuItem exception, boolean visible) {
for (int i=0; i<menu.size(); ++i) {
MenuItem item = menu.getItem(i);
if (item != exception) item.setVisible(visible);
}
}
这篇关于展开搜索视图以使用整个操作栏(隐藏其他内容)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!