Styling a SearchView in Android Action Bar

后端 未结 11 1596
小鲜肉
小鲜肉 2020-12-02 20:56

I have a search widget in my Action Bar like this:

\"Search

(1) How do I change

11条回答
  •  离开以前
    2020-12-02 21:19

    @Override
        public boolean onCreateOptionsMenu(Menu menu) {
    
            super.onCreateOptionsMenu(menu);
    
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
    
            // Associate search configuration with the SearchView
            SearchManager searchManager =
                   (SearchManager) getSystemService(Context.SEARCH_SERVICE);
            SearchView searchView =
                    (SearchView) menu.findItem(R.id.search).getActionView();
            searchView.setQueryHint("Client/DOB/PPSN");
            searchView.setSearchableInfo(
                    searchManager.getSearchableInfo(getComponentName()));
    
            // Setting the textview default behaviour properties
            int id = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
            TextView textView = (TextView) searchView.findViewById(id);
            textView.setTextColor(Color.WHITE);
            textView.setHintTextColor(Color.WHITE);
    
            // Set the search plate color to white
            int linlayId = getResources().getIdentifier("android:id/search_plate", null, null);
            View view = searchView.findViewById(linlayId);
            Drawable drawColor = getResources().getDrawable(R.drawable.searchcolor);
            view.setBackground( drawColor );
            return super.onCreateOptionsMenu(menu);
    
        }
    

    Now this is the xml file searchcolor.xml for changing the plate color of search bar

    
    
      
          
              
          
      
    
      
      
          
              
          
      
    
      
      
          
              
          
      
    
    

    and menu/menu.xml for creating the search in action bar

    
        
    
    
    

提交回复
热议问题