:
Custom spinner actually consists of two parts:
The first part is used to open the drop-down list of buttons, this green background directly set spinner background on the line, the material file as follows:
Note that the text in the spinner control does not directly modify the text interface, the text is actually set in the adapter, for example:
arrayadapter<string> adapter = new Arrayadapter<string> (GetContext (),
r.layout.spinner_checked_text, gradelist);
Gradespinner.setadapter (adapter);
The implementation of this spinner_checked_text.xml is as follows:
<?XML version= "1.0" encoding= "Utf-8"?><Checkedtextviewxmlns:android= "Http://schemas.android.com/apk/res/android"Android:id= "@android: Id/text1"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:gravity= "Center_vertical"android:padding= "10DP"Android:singleline= "true"Android:textcolor= "@color/text_green"android:textsize= "20SP" ></Checkedtextview>
It must be checkedtextview, or it will go wrong.
The second part is the pull-out window, this is the Getdropdownview () method of the replication Arrayadapter to achieve, the code is as follows:
arrayadapter<string> adapter =NewArrayadapter<string>(GetContext (), R.layout.spinner_checked_text, gradelist) {@Override PublicView Getdropdownview (intposition, View Convertview, ViewGroup parent) {View View=Inflate (GetContext (), R.layout.spinner_item_layout,NULL); TextView label=(TextView) view. Findviewbyid (R.id.spinner_item_label); ImageView Check=(ImageView) view. Findviewbyid (R.id.spinner_item_checked_image); Label.settext (Gradelist.get (position)); if(gradespinner.getselecteditemposition () = =position) {View.setbackgroundcolor (Getresources (). GetColor (R.color.spinner_green)); Check.setimageresource (r.drawable.check_selected); } Else{View.setbackgroundcolor (Getresources (). GetColor (R.color.spinner_ligh T_green)); Check.setimageresource (R.drawable.check_unselect); } returnview; } }; Adapter.setdropdownviewresource (r.layout.spinner_item_layout);
The contents of Spinner_item_layout.xml are as follows:
<?XML version= "1.0" encoding= "Utf-8"?><Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:background= "@color/spinner_light_green"android:padding= "15DP" > <TextViewAndroid:id= "@+id/spinner_item_label"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignparentleft= "true"android:layout_centervertical= "true"Android:textcolor= "@color/text_green"android:textsize= "17SP" /> <ImageViewAndroid:id= "@+id/spinner_item_checked_image"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignparentright= "true"android:layout_centervertical= "true"android:src= "@drawable/check_unselect" /></Relativelayout>