Customization of Spinner

Source: Internet
Author: User

First, add a <Spinner/> in the Main.xml

Main.xml

1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 Tools:context= "Com.jorlee.myspinner.MainActivity" >6 7     <Spinner8         Android:id= "@+id/mspinner"9 Android:layout_width= "Match_parent"Ten Android:layout_height= "50DP" One Android:layout_margin= "20DP" A Android:animationcache= "true" - Android:background= "@drawable/mspinner_style"/> -      the </LinearLayout>

Here, I wrote a background myself.

Mspinner_style.xml

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Shapexmlns:android= "Http://schemas.android.com/apk/res/android">3     <Stroke4         Android:width= "0.5DP"5 Android:color= "#FFC0CB"/>6     <Corners7         Android:radius= "10DP"/>8     <Solid9         Android:color= "#FFB6C1"/>Ten </Shape>

And then there's Mainactivity.java.

Mainactivity.java

1  Public classMainactivityextendsActivity {2     PrivateSpinner Mspinner;3     Privatestring[] Mitem;4 @Override5     protected voidonCreate (Bundle savedinstancestate) {6         Super. OnCreate (savedinstancestate);7 Setcontentview (r.layout.activity_main);8         9Mspinner=(Spinner) Findviewbyid (r.id.mspinner);Tenmitem=getresources (). Getstringarray (R.array.spinneritem); One          AArrayadapter<string> arrayadapter=NewArrayadapter<> ( This,  - R.layout.adapter_style, Mitem); - Mspinner.setadapter (arrayadapter); the          -Mspinner.setonitemselectedlistener (NewOnitemselectedlistener () { -  - @Override +              Public voidOnitemselected (adapterview<?>Parent, view view, -                     intPositionLongID) { +String pstring=parent.getitematposition (position). toString (); AToast.maketext (mainactivity. This, "You have chosen:" +pstring, 2000). Show (); at             } -  - @Override -              Public voidOnnothingselected (adapterview<?>parent) { -                 //TODO auto-generated Method Stub -                  in             } -              to         }); +     } -  the}


Here I'm going to show what needs to be shown in the spinner is represented by a simple string array, in the Res/values directory

M_spinner_item.xml

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Resources>3     <String-arrayname= "Spinneritem">4         <Item>Basketball</Item>5         <Item>Football</Item>6         <Item>Badminton</Item>7         <Item>Volleyball</Item>8         <Item>Table tennis</Item>9         <Item>Football</Item>Ten     </String-array> One </Resources>

In the above Java code is not difficult to find, in fact, the so-called spinner customization, the key is the adapter adapter settings, here for convenience, I use the simplest arrayadapter

arrayadapter<string> arrayadapter=New arrayadapter<> (this, R.layout.adapter_style, Mitem);

The second parameter here is the style of each item, in the Res/layout directory

Adapter_style.xml

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <TextViewxmlns:android= "Http://schemas.android.com/apk/res/android"3 style= "? Android:attr/spinnerdropdownitemstyle"4 Android:singleline= "true"5 Android:layout_width= "Fill_parent"6 android:textsize= "25SP"7 Android:textcolor= "#DC143C"8 Android:layout_margin= "10DP"9 Android:layout_height= "? Android:attr/listpreferreditemheight"/>

Visible, each item is a simple TextView

Of course, there are many other adapters that can be used, such as Baseadapter

1Baseadapter baseadapter=NewBaseadapter () {2             3 @Override4              PublicView GetView (intposition, View Convertview, ViewGroup parent) {5Layoutinflater Inflater=layoutinflater.from (mainactivity. This);6Convertview=inflater.inflate (R.layout.mitem,NULL);7                 returnConvertview;8             }9             Ten @Override One              Public LongGetitemid (intposition) { A                 //TODO auto-generated Method Stub -                 return0; -             } the              - @Override -              PublicObject GetItem (intposition) { -                 //TODO auto-generated Method Stub +                 return NULL; -             } +              A @Override at              Public intGetCount () { -                 //TODO auto-generated Method Stub -                 return5; -             } -};

Just in the GetView () method, just

Layoutinflater Inflater=layoutinflater.from (mainactivity. This);  

Convertview=inflater.inflate (R.layout.mitem,null);
Where Mitem.xml is an XML file written in the Res/layout directory, it is the layout of each item and can be customized.

Finally, just be mspinner.setadapter (baseadapter);

Paste on:

When using Arrayadapter:

When using Baseadapter:

Customization of Spinner

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.