Tonight I saw the next spinner. The use of the control, combined with the blog of the Great God Tutorial, a small demo
One, spinneractivity
PrivateSpinner Spinner; PrivateArrayadapter<string>adapter; PrivateList<string>list; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_spinner); List=NewArraylist<string>(); Spinner=(Spinner) Findviewbyid (R.id.spinner); Initlistdata (); Adapter=NewArrayadapter<string> ( This, Android. R.layout.simple_list_item_1,list); Adapter.setdropdownviewresource (r.layout.spinner_dropdownlayout); Spinner.setadapter (adapter); Spinner.setonitemselectedlistener (NewAdapterview.onitemselectedlistener () {@Override Public voidOnitemselected (adapterview<?> Parent, view view,intPositionLongID) {String add=Adapter.getitem (position); Toast.maketext (Getbasecontext (), Add,toast.length_long). Show (); } @Override Public voidOnnothingselected (adapterview<?>parent) { } }); }
Spinner in the use of the same time as the ListView of the previous study, the need to preload the good data, so also to use the adapter to bind. The demo uses a simple arrayadapter that displays a string to illustrate the problem. The layout style used is also the android that comes with the system. R.layout.simple_spinner_item
View the source code, the layout is also very simple, single-line, scrolling, there is a way to set the text alignment
Simple_spinner_item.xml
<textview xmlns:android= "http://schemas.android.com/apk/res/android" android:id= "@android: ID/ Text1 " style="? Android:attr/spinneritemstyle " android:singleline=" true " Android:layout_width= "Match_parent" android:layout_height= "Wrap_content" android: Ellipsize= "marquee" android:textalignment= "Inherit"/>
Another place to note is Setdropdownviewresource, the function used to set the drop-down menu style, in the demo with a custom drop-down style, the internal layout is a simple textview, set the text size, text color and other properties. Of course the success shows.
If the drop-down style is not set, the style is the same as the spinner style.
Spinner there are two ways to display a frame, one is a drop-down, which can be set by property.
Android:spinnermode= "dropdown"android:spinnermode= "Dialog"
Spinner_dropdownlayout.xml
<?xml version= "1.0" encoding= "Utf-8"?><textviewxmlns:android = "http://schemas.android.com/apk/ Res/android "android:id=" @+id/textview5 "android:layout_width=" Wrap_content "android:layout _height= "24SP"android:singleline= "true"android:textcolor= "#045556"style= "? Android:attr/spinnerdropdownitemstyle "/>
If you need to display the picture, the text is mixed with the style, then use the Simpleadapter adapter, just like the previous section, and then the drop-down style is also set to the same.
The use of Android learning spinner controls