Describes how to modify the Spinner style of an Android instance.
For android project development, it is often used to implement the drop-down box effect of the Spinner. The following methods can be used to load an adapter from a Spinner:
1. Directly load the ArrayAdapter and SimpleAdapter provided by android;
2. Customize the adapter that inherits the BaseAdapter.
For the adapter to load a custom xml layout file, it is easier to modify the Spinner style, that is, to modify the display style in the defined xml layout file. However, for loading xml layout files that come with android, sometimes the results are not required by the project. The main problems are as follows:
1. The background display style of the Spinner;
2. display style of the text box in the Spinner;
3. display the style in the drop-down menu of the Spinner;
The following examples solve the style issues mentioned above:
Package com. example. spinnerdemo; import android. app. activity; import android. OS. bundle; import android. widget. arrayAdapter; import android. widget. spinner; public class MainActivity extends Activity {private Spinner spinner; private Spinner spinnerTwo; private String [] datas; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); spinner = (Spinner) this. findViewById (R. id. spinner); spinnerTwo = (Spinner) this. findViewById (R. id. spinnerTwo); datas = new String [] {Zhang San, Li Si, Wang Wu, Zhao Liu}; // the original style is android. r. layout. simple_spinner_dropdown_item is used as an example. Other modifications are similar to ArrayAdapter.
Adapter = new ArrayAdapter
(This, android. r. layout. simple_spinner_dropdown_item, datas); adapter. setDropDownViewResource (android. r. layout. simple_spinner_dropdown_item); spinner. setAdapter (adapter); // custom style changed based on the original ecological style // ArrayAdapter
AdapterTwo = new ArrayAdapter
(This, R. layout. my_simple_spinner_self_item, datas); // display the style adapterTwo. setDropDownViewResource (R. layout. Adjust); spinnerTwo. setAdapter (adapterTwo );}}
In MainActivity. java, android. R. layout. simple_spinner_dropdown_item is used as an example. The xml layout file of android. R. layout. simple_spinner_dropdown_item is as follows:
We need to implement the style to be implemented above, and we only need to modify it On the basis.
1. Modify the background color of the Spinner.
A. Set the background color selector spinner_selector.xml
B. Set color. xml
#FFFFFF
#0000FF
C. Set activity_main.xml for the background color.
2. text display style in the Spinner
3. display style in the Spinner drop-down box