How do I open the SearchView programmatically?

后端 未结 8 1610
既然无缘
既然无缘 2020-12-02 09:19

There is this widget for the ActionBar which called \'SearchView\'. When it\'s not in use, it looks like this:

\

相关标签:
8条回答
  • 2020-12-02 09:37

    If you want to use support library only when necessary, do this

        MenuItem searchMenuItem = menu.findItem(R.id.action_search);
        if (Utils.hasIceCreamSandwich())
            searchMenuItem.expandActionView();
        else MenuItemCompat.expandActionView(searchMenuItem);
    

    else simply do this

        MenuItem searchMenuItem = menu.findItem(R.id.action_search);
        MenuItemCompat.expandActionView(searchMenuItem);
    
    0 讨论(0)
  • 2020-12-02 09:42

    Expand the SearchView with

    searchView.setIconified(false);
    

    and collapse it with

    searchView.setIconified(true);
    

    You need to change the value of android:showAsAction from ifRoom|collapseActionView to always. The SearchView's attribute android:iconifiedByDefault should be true, which is the default value, otherwise the user can not collapse the SearchView after it was expanded programmatically.

    0 讨论(0)
提交回复
热议问题