当在android的默认SearchView
中将iconifide设置为false时,SearchView
的关闭按钮被隐藏。
要求:启动SearchView
时,默认情况下应打开Activity
,并且应该显示关闭按钮;
searchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this, SearchResultActivity.class)));
searchView.setIconifiedByDefault(false);
最佳答案
您可以做到的第一种方法
SearchView searchView = (SearchView)
menu.findItem(R.id.action_search).getActionView();
searchView.setIconifiedByDefault(true);
searchView.setFocusable(true);
searchView.setIconified(false);
searchView.requestFocusFromTouch();
或有另一种方法
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.search, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
try {
Field searchField = SearchView.class.getDeclaredField("mCloseButton");
searchField.setAccessible(true);
mSearchCloseButton = (ImageView) searchField.get(mSearchView);
if (mSearchCloseButton != null) {
mSearchCloseButton.setEnabled(false);// mSearchCloseButton.setEnabled(false);
mSearchCloseButton.setImageDrawable(getResources().getDrawable(R.drawable.transparent));
}
} catch (Exception e) {
Log.e(TAG, "Error finding close button", e);
}
}
有关更多详细信息,link-1,link-1
关于android - 搜索 View 关闭按钮被隐藏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34527527/