10

使用操作栏搜索界面时,小部件在纵向模式下会展开以占据屏幕的整个宽度,但在横向模式下会停止。

有没有办法在 SearchView 上设置扩展布局参数以在用户键入搜索时完全填充操作栏?

见图片:

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;
        }
    }

});
4

6 回答 6

17

MenuItemCompat's SearchView has a property named maxWidth.

    final MenuItem searchItem = menu.findItem(R.id.action_search);
    final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    searchView.setMaxWidth(xxx);

use screen width instead of xxx offcourse

于 2015-07-30T11:26:43.167 回答
4

你尝试过这样的事情吗?

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;
    }

});
于 2013-04-07T21:35:20.143 回答
0

我想出了一个解决方案,可以在横向中完全扩展搜索视图,并且在创建活动时也已经扩展了操作视图。它是如何工作的:

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>

Note: "@string/search" - looks something like this in the res-strings.xml:

    <string name="search">Search</string>

2.Second create the layout referred above ("@layout/searchview_layout") in res-layout folder. The new layout: searchview_layout.xml will look like this:

    <?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"/>

Note: Here we are setting the search view width to match the width of its parent( android:layout_width="match_parent")

3.In your MainActivity class or in the activity that has to implement the Search View write in the onCreateOptionsMenu() method the following code:

@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;
}
于 2014-03-22T21:42:44.577 回答
0

Adjust the width layout param to MATCH_PARENT, that way the SearchView will fully expand in either portrait or landscape

ViewGroup.LayoutParams layoutParams = mSearchView.getLayoutParams();
layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
于 2014-08-26T20:40:35.923 回答
0

For ActionBarCompat use this:

    MenuItemCompat.setShowAsAction(searchItem, MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
            | MenuItem.SHOW_AS_ACTION_ALWAYS);
    MenuItemCompat.expandActionView(searchItem);
于 2014-09-19T17:01:55.633 回答
0

Use this (100%):

mSearchView = (SearchView) searchItem.getActionView();    
mSearchView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
于 2014-09-27T06:47:51.477 回答