The spinner style that comes with the Android system is far from satisfying the spinner UI style requirements in our actual development process, so we definitely need to modify our spinner style to suit the style of the application. The system gives us two kinds of common modification methods, one is to use the XML static, the other is the Java code dynamic to modify, our article is mainly about how to dynamically modify the style of spinner. My implementation method, is to construct a spinneradapter, inherit from Arrayadapter, rewrite Getdropdownview (), GetView () These two methods are good, the other method is not critical, It's not much different from the other ListView. So now let's just look at the code parsing!
Package Com.example.account.add;import Android.content.context;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.arrayadapter;import Android.widget.TextView ;p ublic class Spinneradapter extends arrayadapter<string> {private Context mcontext;private string[] Mstringarray ;p ublic Spinneradapter (context context, string[] stringarray) {Super (context, Android. R.layout.simple_spinner_item, stringarray); mcontext = Context;mstringarray = Stringarray;} @Overridepublic view Getdropdownview (int position, view Convertview, ViewGroup parent) {//modify spinner expanded font color if ( Convertview = = null) {Layoutinflater inflater = Layoutinflater.from (Mcontext);//We can also load our own layout layouts Convertview = Inflater.inflate (Android. R.layout.simple_spinner_dropdown_item, parent,false);} TextView TV = (TextView) Convertview.findviewbyid (Android. R.ID.TEXT1); Tv.settext (Mstringarray[position]); Tv.settextsize (); return convertview;} @Overridepublic View getView (int poSition, View Convertview, ViewGroup parent) {//Modify spinner selection result font Color if (Convertview = = null) {Layoutinflater Inflater = Lay Outinflater.from (mcontext); Convertview = Inflater.inflate (Android. R.layout.simple_spinner_item, parent, false);} Here Text1 is the spinner system for displaying text Textviewtextview TV = (TextView) Convertview.findviewbyid (Android. R.ID.TEXT1); Tv.settext (Mstringarray[position]); Tv.settextsize (); return convertview;}}
Implementation of custom spinner styles for Android development (source code implementation)