The list items for the Spinner drop-down list box are configured in two ways:
1. Through the resource file configuration, the list item content is added by using the <string-array> element in the XML of the values, such as Strings.xml, and then through the android:entries= "@array/xxid" The way to link.
2. Read the resource file through the Android.widget.ArrayAdapter class or specify the specific settings data.
One: How the resource files are configured.
<?XML version= "1.0" encoding= "Utf-8"?><Resources> <stringname= "App_name">ADTest</string> <stringname= "Hello_world">Hello world!</string> <stringname= "Action_settings">Settings</string> <String-arrayname= "Mycity"> <Item>Beijing</Item> <Item>Chongqing</Item> <Item>Shanghai</Item> </String-array></Resources>
Configure the link through the android:entries= "@array/mycity" of the spinner species.
Two. Configure through the Arrayadapter class.
(1) Read the list items defined in the resource file
arrayadapter<charsequence> adapter = Arrayadapter.createfromresource (This, r.array.mycity, Android. R.layout.simple_spinner_item); Adapter.setdropdownviewresource (Android. R.layout.simple_spinner_dropdown_item); Spinner.setadapter (adapter);
(2) Read the development data
arraylist<charsequence> list =NewArraylist<charsequence>(); List.add (Beijing); List.add (Chongqing); List.add (Shanghai); List.add (Tianjin); Spinner Spinner=(Spinner) Findviewbyid (r.id.spinner1); Arrayadapter<CharSequence> adapter =NewArrayadapter<charsequence> ( ThisAndroid. R.layout.simple_spinner_dropdown_item, list); Adapter.setdropdownviewresource (Android. R.layout.simple_spinner_item); Spinner.setadapter (adapter);
Or
string[] items = {"Beijing", "Shanghai", "Chongqing", "Tianjin"};
Spinner Spinner = (Spinner) Findviewbyid (r.id.spinner1);
arrayadapter<charsequence> adapter = New Arrayadapter<charsequence> (this, Android. R.layout.simple_spinner_dropdown_item, items);
Adapter.setdropdownviewresource (Android. R.layout.simple_spinner_item);
Spinner.setadapter (adapter);
Where the charsequence is the parent class interface of String,
Arrayadapter<charsequence> adapter = new arrayadapter<charsequence> ( Span style= "color: #0000ff;" >this, Android. R.layout.simple_spinner_dropdown_item, items) in Android. R.layout.simple_spinner_dropdown_item is the style when the list box is not expanded, while Adapter.setdropdownviewresource (Android. R.layout.simple_spinner_item) is the style of the drop-down list after the settings are expanded.