SearchView taking all the space in the new ActionBarCompat

前端 未结 3 598
广开言路
广开言路 2020-12-09 04:29

I switched from ActionBarSherlock to ActionBarCompat (support library v7). After some adjustments, almost everything is working fine by now.

But I\'m in trouble with

3条回答
  •  无人及你
    2020-12-09 04:58

    Have you tried using collapseActionView()?

    I use it like this:

    public static MenuItem msearchMenuItem;
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
    
        // Associate searchable configuration with the SearchView
        SearchManager searchManager =
               (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView =
                (SearchView) menu.findItem(R.id.search).getActionView();
    
        searchView.setSearchableInfo(
                searchManager.getSearchableInfo(getComponentName()));
    
    
    
        msearchMenuItem = menu.findItem(R.id.search);
    
        return true;
    }
    public static MenuItem getSearchMenuItem() {
        return msearchMenuItem;
    }
    
    public void doSomething(){
        //Collapse the SearchBar
        getSearchMenuItem().collapseActionView();
    }
    

    I don't know if it works with v7, but it certainly works with v4.

    Try changing android:showAsAction="collapseActionView|ifRoom"

    
    
    
    

提交回复
热议问题