Yesterday in the company project encountered spinner color, font, size confusion. So let's summarize this knowledge today. This is actually very simple, to change the font, color and size, is not to use the system comes with the style. You can write a layout call on your own exception. That's how I do it. Examples of this article are spinner two pop-up styles, drop-down boxes, and pop-up boxes. Two ways to load a data source. See the code below for an introduction.
This article source code address: http://download.csdn.net/detail/qq_16064871/8583085
Reprint Please specify the Source: http://blog.csdn.net/qq_16064871
Package Com.example.customspinerdemo;import Java.util.arraylist;import Java.util.list;import android.os.Bundle; Import Android.app.activity;import android.view.menu;import android.view.view;import Android.widget.AdapterView; Import Android.widget.adapterview.onitemselectedlistener;import Android.widget.arrayadapter;import Android.widget.spinner;import Android.widget.toast;public class Mainactivity extends Activity implements Onitemselectedlistener {private Spinner mspinner1;private Spinner mspinner2; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); mspinner1 = ( Spinner) Findviewbyid (r.id.spinner1); if (mspinner1! = null) {Mspinner1.setonitemselectedlistener (this);} Mspinner2 = (Spinner) Findviewbyid (R.ID.SPINNER2); if (mspinner2! = null) {Mspinner2.setonitemselectedlistener (this);} Here are two layout self-defined effects, without the system comes with.//Data source manually added arrayadapter<string> Spinneraadapter = new Arrayadapter<string> ( This,r.layout.Custom_spiner_text_item, Getdatasource ()); Spinneraadapter.setdropdownviewresource (R.layout.custom_spinner_ Dropdown_item); Mspinner1.setadapter (spinneraadapter);//data source configured by loading XML file Arrayadapter SpinnerAadapter2 = Arrayadapter.createfromresource (This,r.array.spiner2_array, R.layout.custom_spiner_text_item); Spinneraadapter2.setdropdownviewresource (R.layout.custom_spinner_dropdown_item); Mspinner2.setAdapter ( SPINNERAADAPTER2);} Public list<string> Getdatasource () {list<string> spinnerlist = new arraylist<string> (); Spinnerlist.add ("Beijing"), Spinnerlist.add ("Shanghai"), Spinnerlist.add ("Guangzhou"), Spinnerlist.add ("Beijing"); Spinnerlist.add ("Shanghai"); Spinnerlist.add ("Guangzhou"); return spinnerlist;} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;} @Overridepublic void onitemselected (adapterview<?> arg0, View arg1, int arg2,long arg3) {if (Arg0.getid () = = R.id.sp Inner1) {StRing itemstring = Mspinner1.getitematposition (arg2). toString (); Toast.maketext (This, "You have selected" + itemstring, Toast.length_short). Show (); return;} else if (arg0.getid () = = R.id.spinner2) {String itemString2 = mspinner2.getitematposition (arg2). toString (); Toast.maketext (This, "You have selected" + ItemString2, Toast.length_short). Show (); return;}} @Overridepublic void onnothingselected (adapterview<?> arg0) {}}
Custom_spiner_text_item.xml
<textview xmlns:android= "http://schemas.android.com/apk/res/android" android:id= "@+id/textviewcustom" style= "? Android:attr/spinneritemstyle" android:layout_width= "match_parent" android:layout_height= "Wrap_content" android:ellipsize= "marquee" android:singleline= "true" android:textalignment= " Inherit " android:textcolor=" #222288 " android:textsize=" 16sp "/>
Custom_spinner_dropdown_item.xml
<checkedtextview xmlns:android= "http://schemas.android.com/apk/res/android" android:id= "@+id/ Checkedtextviewcustom " style="? Android:attr/spinnerdropdownitemstyle " android:layout_width=" Match_ Parent " android:layout_height=" 50DP " android:ellipsize=" marquee " android:singleline=" true " Android:textalignment= "Inherit" android:textcolor= "#222288" android:textsize= "16sp"/>
Activity_main.xml
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:orien tation= "vertical" > <linearlayout android:layout_width= "match_parent" android:layout_height= "60DP" android:gravity= "Center_vertical" > <textview android:id= "@+id/textview1" Androi D:layout_width= "116DP" android:layout_height= "wrap_content" android:text= "Custom drop-down box:" Androi D:textsize= "16sp"/> <spinner android:id= "@+id/spinner1" android:layout_width= "Wrap_con Tent "android:layout_height=" Wrap_content "android:layout_weight=" 1 "/> </LinearLayout> <linearlayout android:layout_width= "match_parent" android:layout_height= "Wrap_content" Android: gravity= "Center_vertical" > <textvIew android:id= "@+id/textview2" android:layout_width= "116DP" android:layout_height= "Wrap_ Content "android:text=" Custom pop-up box: "android:textsize=" 16sp "/> <spinner android: Id= "@+id/spinner2" android:layout_width= "202DP" android:layout_height= "Wrap_content" Android oid:spinnermode= "dialog"/> </LinearLayout></LinearLayout>
The default is the drop-down box when configuring spinner in XML.
If you want to change the pop-up box, add this android:spinnermode= "dialog"
Trigger Spinner statement with other listeners: Mspinner.performclick ();
This article source code address: http://download.csdn.net/detail/qq_16064871/8583085
Reprint Please specify the Source: http://blog.csdn.net/qq_16064871
Android Custom Spinner font, color, size