Http://www.cnblogs.com/coding-way/p/3549865.html
Present effect:
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"? ><checkedtextview xmlns: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 = new Arrayadapter<string> (GetContext (), R.layout.spinner_ Checked_text, gradelist) {@Override public view getdropdownview (int position, 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 (). Getcol or (R.color.spinner_green)); Check.setimageresource (r.drawable.check_selected); } else {View.setbackgroundColor (Getresources (). GetColor (R.color.spinner_light_green)); Check.setimageresource (R.drawable.check_unselect); } return view; } }; Adapter.setdropdownviewresource (r.layout.spinner_item_layout);
The contents of Spinner_item_layout.xml are as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns: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" > <textview android: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 "/> <imageview android: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>
Android Custom Spinner and its drop-down window