使用操作栏搜索界面时,窗口小部件将以纵向模式展开以占据屏幕的整个宽度,但在横向模式下会停止缩短。
有没有办法在SearchView上设置扩展布局参数,以便在用户键入搜索时完全填充操作栏?
见图:
注意:我目前没有使用ActionBar Sherlock
编辑:这是我为了扩展全宽而做的。
searchView.setOnSearchClickListener(new OnClickListener() {
private boolean extended = false;
@Override
public void onClick(View v) {
if (!extended) {
extended = true;
LayoutParams lp = v.getLayoutParams();
lp.width = LayoutParams.MATCH_PARENT;
}
}
});
你尝试过类似的东西吗?
editView.setOnKeyListener(new OnKeyListener() {
private boolean extended = false;
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (!extended && event.getAction() == KeyEvent.ACTION_DOWN) {
extended = true;
LayoutParams lp = v.getLayoutParams();
lp.width = LayoutParams.MATCH_PARENT;
}
return false;
}
});
MenuItemCompat的SearchView有一个名为maxWidth的属性。
final MenuItem searchItem = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setMaxWidth(xxx);
使用屏幕宽度而不是xxx offcourse
我想出了一个解决方案,可以在横向上完全展开搜索视图,并且在创建活动时也可以扩展操作视图。它是如何工作的:
1.首先在res-menu文件夹中创建一个xml文件,例如:searchview_in_menu.xml。在这里,您将拥有以下代码:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/action_search"
android:title="@string/search"
android:icon="@android:drawable/ic_menu_search"
android:actionLayout="@layout/searchview_layout" />
</menu>
注意:“@ string / search” - 在res-strings.xml中看起来像这样:
<string name="search">Search</string>
2.Second在res-layout文件夹中创建上面提到的布局(“@ layout / searchview_layout”)。新布局:searchview_layout.xml将如下所示:
<?xml version="1.0" encoding="utf-8"?>
<SearchView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/search_view_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
注意:这里我们设置搜索视图宽度以匹配其父级的宽度(android:layout_width =“match_parent”)
3.在MainActivity类或必须实现搜索视图的活动中,在onCreateOptionsMenu()方法中编写以下代码:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.searchview_in_menu, menu);
//find the search view item and inflate it in the menu layout
MenuItem searchItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) searchItem.getActionView();
//set a hint on the search view (optional)
mSearchView.setQueryHint(getString(R.string.search));
//these flags together with the search view layout expand the search view in the landscape mode
searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
| MenuItem.SHOW_AS_ACTION_ALWAYS);
//expand the search view when entering the activity(optional)
searchItem.expandActionView();
return true;
}
将宽度布局参数调整为MATCH_PARENT
,这样SearchView
将以纵向或横向完全展开
ViewGroup.LayoutParams layoutParams = mSearchView.getLayoutParams();
layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
对于ActionBarCompat,使用此:
MenuItemCompat.setShowAsAction(searchItem, MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
| MenuItem.SHOW_AS_ACTION_ALWAYS);
MenuItemCompat.expandActionView(searchItem);
使用此(100%):
mSearchView = (SearchView) searchItem.getActionView();
mSearchView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));