Android drop-down menu, such as <select> in HTML, the key is to call the Setdropdownviewresource method, in XML to define the appearance of the Drop-down menu to display
Steps:
1. Define spinner control
Copy Code code as follows:
<spinner
Android:id= "@+id/spinner"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:prompt= "@string/planet_prompt"/><!--a string resource, so point to a string-->
2. Create a new Android XML file named arrays under the Res/values/folder
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<string-array name= "planets" >
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Neptune</item>
</string-array>
</resources>
The contents of the file are automatically added to the R file, referenced by: r.array.planets; This is the list of user Drop-down selections;
3. Add the following code to the OnCreate method of the activity
Copy Code code as follows:
Spinner s = (Spinner) Findviewbyid (R.id.spinner);
Arrayadapter Adapter2 = Arrayadapter.createfromresource (this , r.array.planets, Android. R.layout.simple_spinner_item);
Adapter2.setdropdownviewresource ( android. R.layout.simple_spinner_dropdown_item);
S.setadapter (ADAPTER2);
Ok!