如何使 SearchView 始终在 android 中展开?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24122448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make SearchView always expanded in android?
提问by ruruma
I have SearchView
in the top of the layout (not in the action bar), is there anyway to force this View
to be always expanded (opened)?
我SearchView
在布局的顶部(不在操作栏中),无论如何要强制它View
始终展开(打开)?
If not, i wish to place fancy image near it, is there anyway to make SearchView
hide this image when expanded (clicked/expanded)?
如果没有,我希望在它附近放置精美的图像,无论如何SearchView
在展开(单击/展开)时隐藏此图像?
回答by Wakim
You can use the property android:iconifiedByDefault="false"
on XML or set programatically setIconifiedByDefault(false)
. Acording to the documentation this property set the SearchView
expanded like you want.
您可以android:iconifiedByDefault="false"
在 XML 上使用该属性或以编程方式设置setIconifiedByDefault(false)
。根据文档,此属性设置为SearchView
您想要的扩展。
Take a look at SearchView.setIconifiedByDefault(boolean iconified)
回答by Pankaj Arora
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
searchItem.expandActionView();
and in menu file, use
并在菜单文件中,使用
showAsAction="ifRoom|collapseActionView"
If any query, feel free to comment.
如果有任何疑问,请随时发表评论。
回答by yoAlex5
You can try to use
你可以尝试使用
searchView.onActionViewExpanded();
and if you do not need keyboard opened
如果您不需要打开键盘
searchView.clearFocus();
回答by dharmx
I am doing this into fragment , i done it by using
我正在将其放入片段中,我使用
searchItem.expandActionView();
but when user press back button it collapse so in collapse method i used
但是当用户按下后退按钮时它会崩溃,所以在我使用的折叠方法中
getActivity().getSupportFragmentManager().popBackStack();
// when it collapsed i am going back
Below is my solution in detail:
以下是我的详细解决方案:
search_menu.xml
search_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/search_contacts"
android:title="search"
android:icon="@drawable/search2"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" >
</item>
</menu>
in fragment i used below code :
在片段中我使用了以下代码:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.search_menu, menu);
// Define the listener
MenuItemCompat.OnActionExpandListener expandListener = new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionCollapse(MenuItem item)
{
getActivity().getSupportFragmentManager().popBackStack();
// Do something when action item collapses
return true; // Return true to collapse action view
}
@Override
public boolean onMenuItemActionExpand(MenuItem item)
{
// Do something when expanded
return true; // Return true to expand action view
}
};
MenuItem searchItem = menu.findItem(R.id.search_contacts);
// Assign the listener to that action item
MenuItemCompat.setOnActionExpandListener(searchItem, expandListener);
// Any other things you have to do when creating the options menu…
SearchView searchView =
(SearchView) MenuItemCompat.getActionView(searchItem);
//searchView.setIconifiedByDefault(false);
searchItem.expandActionView(); // when fragment opens it expanded auto
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText)
{
seachViewFunction(newText); // in searchViewFunction(newText); i am doing my things
return false;
}
});
super.onCreateOptionsMenu(menu, inflater);
}