Describes how to modify the Spinner style of an Android instance.

Source: Internet
Author: User

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
 
 
 
 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.