Implementation of custom spinner styles for Android development (source code implementation)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.