first, Send on:
Second, XML
<relativelayout 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:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_ Horizontal_margin " android:paddingright=" @dimen/activity_horizontal_margin " android:paddingtop=" @dimen /activity_vertical_margin " tools:context=". Mainactivity " android:background=" @drawable/blue " android:id=" @+id/main " android:padding=" 0DP " > <spinner android:id= "@+id/spinner" android:layout_width= "Fill_parent " android:layout_height= "Wrap_content" android:prompt= "@string/color" /></relativelayout>
Third, the code implementation:
Package Com.example.spinnerdemo;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.view.Window;import Android.widget.adapterview;import Android.widget.adapterview.onitemselectedlistener;import Android.widget.arrayadapter;import Android.widget.relativelayout;import Android.widget.spinner;import Android.widget.toast;public class Mainactivity extends Activity {private Spinner spinner;private relativelayout Relativelayout;private arrayadapter<string> adapter;private int[] resource = {r.drawable.blue,r.drawable.red, R.drawable.green};p rotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title); Setcontentview (r.layout.activity_main); relativeLayout = ( Relativelayout) Findviewbyid (r.id.main), spinner = (spinner) This.findviewbyid (R.id.spinner); Spinner.setprompt (" Set background color "); adapter = new Arrayadapter<striNg> (this, Android. R.layout.simple_spinner_item,getdata ()); Adapter.setdropdownviewresource (Android. R.layout.simple_spinner_dropdown_item); Spinner.setadapter (adapter); Spinner.setselection (0,true);//jump directly to a specified data Spinner.setonitemselectedlistener (new Selectedlistener ());} /** * Data source * @return */private list<string> getData () {list<string> items = new arraylist<string> (); items . Add ("Blue"); Items.Add ("Red"); Items.Add ("green"); return items;} /** * Dropdown Options Click event * @author Administrator * */final class Selectedlistener implements Onitemselectedlistener{public void OnIt Emselected (adapterview<?> arg0, View arg1, int arg2,long arg3) {list<string> List = GetData (); relativelayout . Setbackgroundresource (Resource[arg2]); Toast (List.get (arg2));} /** * Callback function that executes when the selected entries in the View all disappear. * Startup touch or empty adapter may cause the selected entry to disappear. */public void onnothingselected (adapterview<?> arg0) {}}/** * custom toast * @param msg */public void Toast (String msg) { Toast.maketext (This, MSG, Toast. Length_short). Show ();} Menu public boolean oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds items to the action bar if it is present . Getmenuinflater (). Inflate (R.menu.main, menu); return true;}}
Implementation of the Android drop-down menu (Spinner)