Android SearchView custom SearchIcon and font color size, searchviewicon
The search icons and font attributes of custom SearchView are more complex.
1. Custom SearchIcon
1. If the API version is earlier than 21: if the version is earlier than 21, it is complicated to modify the SearchIcon. You must first obtain the ImageView of the SearchView and then set the image for the ImageView. The Code is as follows:
(1) initialize the SearchView Control
mSearch = (SearchView) view.findViewById(R.id.search);
(2) Set Custom Search icons
If (mSearch = null) {return;} else {// obtain the ImageView idint imgId = mSearch. getContext (). getResources (). getIdentifier ("android: id/search_mag_icon", null, null); // obtain ImageViewImageView searchButton = (ImageView) mSearch. findViewById (imgId); // sets the image searchButton. setImageResource (R. drawable. search); // do not use the default mSearch. setIconifiedByDefault (false );}
2. When the API version is greater than 21, it is very convenient to directly set the property searchIcon for SearchView in the layout file.
android:searchIcon="@drawable/search"
2. Customize the font color and size. You can also modify the color of the prompt text in SearchView.
1. initialize the SearchView control.
2. Get the TextView of SearchView and modify its attributes. The Code is as follows.
If (mSearch = null) {return;} else {// obtain the TextView IDint id = mSearch. getContext (). getResources (). getIdentifier ("android: id/search_src_text", null, null); // obtain the TextView control TextView textView = (TextView) mSearch. findViewById (id); // set the font size to 14sptextView. setTextSize (TypedValue. COMPLEX_UNIT_SP, 14); // 14sp // set the font color textView. setTextColor (getActivity (). getResources (). getColor (R. color. search_txt_color); // sets the text color textView. setHintTextColor (getActivity (). getResources (). getColor (R. color. search_hint_color ));}
Iii. Final: