ActionBar SearchView not fully expanding in landsc

2020-07-07 05:58发布

When using the action bar search interface, the widget expands to occupy the full width of the screen in portrait mode, but stops short in landscape mode.

Is there a way to set the expansion layout params on the SearchView to fully fill the action bar when the user is typing a search?

See image:

SearchView text field not using the full width in landscape mode

Note: I'm not currently using ActionBar Sherlock

Edit: Here's what I did to get it to extend the full width.

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

});

6条回答
beautiful°
2楼-- · 2020-07-07 06:05

Use this (100%):

mSearchView = (SearchView) searchItem.getActionView();    
mSearchView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
查看更多
\"骚年 ilove
3楼-- · 2020-07-07 06:08

I figured out a solution for fully expanding the search view in landscape and to also have the action view already expanded when the activity is created. Here how it works:

1.First create an xml file in your res-menu folder called for example : searchview_in_menu.xml. Here you would have the following code:

    <?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;
}
查看更多
干净又极端
4楼-- · 2020-07-07 06:09

For ActionBarCompat use this:

    MenuItemCompat.setShowAsAction(searchItem, MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
            | MenuItem.SHOW_AS_ACTION_ALWAYS);
    MenuItemCompat.expandActionView(searchItem);
查看更多
劳资没心,怎么记你
5楼-- · 2020-07-07 06:10

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;
查看更多
叼着烟拽天下
6楼-- · 2020-07-07 06:14

Did you try something like that?

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

});
查看更多
我只想做你的唯一
7楼-- · 2020-07-07 06:14

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

查看更多
登录 后发表回答