The key to the drop-down menu in Android, such as <select> in html, is to call the setDropDownViewResource method to define the display appearance of the drop-down menu in XML format.
Steps:
1. Define the Spinner Control
Copy codeThe Code is 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 you must point to a string -->
2. Create an Android XML file named arrays in the res/values/folder.
Copy codeThe Code is 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 content in this file is automatically added to the R file. The reference method is R. array. planets. This is the List of items selected from the drop-down List;
3. Add the following code to the onCreate method of Activity:
Copy codeThe Code is 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!