SearchView taking all the space in the new ActionBarCompat

前端 未结 3 583
广开言路
广开言路 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"

    <menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/search"
          android:title="@string/search_title"
          android:icon="@drawable/ic_search"
          android:showAsAction="collapseActionView|ifRoom"
          android:actionViewClass="android.widget.SearchView" />
    </menu>
    
    0 讨论(0)
  • 2020-12-09 05:03

    Remove the line

    searchView.setIconifiedByDefault(false);
    

    Or you can explicitly call the method with true as the argument.

    0 讨论(0)
  • 2020-12-09 05:11

    This is definitely a bug about Android but a workaround can be including SearchView programmatically like this:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
        searchView.setIconifiedByDefault(false);
        getActionBar().setCustomView(searchView);
        getActionBar().setDisplayShowCustomEnabled(true);
    }
    

    You can also use a layout XML to define SearchView properties. However "iconifiedByDefault" in XML tends to be ineffective in my experience. (This may my bad though)

    Thanks for creating an issue about this. Here's the URL to the related bug report: https://code.google.com/p/android/issues/detail?id=58251

    Despite what is mentioned in the bug report, my experience was the same with both ActionBarSherlock and ActionBarCompat. So I expect that ActionBarSherlock users are also affected.

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