Text input in SearchView doesn't show

后端 未结 2 1545
挽巷
挽巷 2021-01-17 15:49

I noticed a couple days ago that inputting text in my SearchView doesn\'t show up. Maybe this problem started longer ago and I hadn\'t noticed it, but I know th

2条回答
  •  北恋
    北恋 (楼主)
    2021-01-17 16:42

    This is the code I use for my recent project and it's work fine:

    First define search_menu.xml (make sure use android supportV7) :

    
    
        
    
    

    Then define in your onCreateOptionMenu of your Activity:

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_search, menu);
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.search));
    searchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(getApplicationContext(), SearchActivity.class)));
    searchView.setMaxWidth(Integer.MAX_VALUE);
    MenuItemCompat.expandActionView(menu.findItem(R.id.search));
    searchView.setIconifiedByDefault(true);
    searchView.setIconified(false);
    

    Also my Toolbar:

    
    

    Add meta-data to your AndroidManifest.xml:

        
            
                
            
            
        
    

    Final result:

提交回复
热议问题