The custom Searchview search icon and font properties are relatively complex and are noted.
One, custom Searchicon
1, API version is less than 21: version is less than 21 o'clock, to modify Searchicon more complex, you need to get to Searchview ImageView, and then set up a picture for ImageView, the specific code is as follows:
(1) Initializing the Searchview control
Msearch = (Searchview) View.findviewbyid (R.id.search);
(2) Setting a custom Search icon
if(msearch==NULL){ return;}Else{//gets the ID of the ImageViewintImgid = Msearch.getcontext (). Getresources (). Getidentifier ("Android:id/search_mag_icon",NULL,NULL); //Get ImageViewImageView SearchButton =(ImageView) Msearch.findviewbyid (imgid);//set up a pictureSearchbutton.setimageresource (r.drawable.search);//do not use the defaultMsearch.seticonifiedbydefault (false);}
2, API version is more than 21 o'clock, it is very convenient, directly in the layout file for Searchview set properties Searchicon can be.
android:searchicon= "@drawable/search"
Second, custom font color and size, you can also modify the color of the hint text in Searchview
1, initialize the Searchview control, ibid.
2, get to Searchview TextView, then you can modify its properties, the code is as follows.
if(msearch==NULL){ return;}Else{//gets the ID to the TextViewintid = Msearch.getcontext (). Getresources (). Getidentifier ("Android:id/search_src_text",NULL,NULL);//get the control to TextViewTextView TextView =(TextView) Msearch.findviewbyid (ID);//set font size to 14SPTextview.settextsize (TYPEDVALUE.COMPLEX_UNIT_SP, 14);//14SP//Set Font ColorTextview.settextcolor (Getactivity (). Getresources (). GetColor (R.color.search_txt_color));//set hint text colorTextview.sethinttextcolor (Getactivity (). Getresources (). GetColor (R.color.search_hint_color));}
Third, the final:
Android Searchview custom Searchicon and font color size