Android to implement Pull-down menu spinner effect _android

Source: Internet
Author: User

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

1.1.activity_main.xml

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" Match_parent "android:layout_height = "Match_parent" android:orientation= "vertical" tools:context= "com.rj141.sb.kongjian.MainActivity" > <linea Rlayout android:orientation= "Horizontal" android:layout_width= match_parent "android:layout_height=" Wrap_co 
      Ntent "> <textview android:layout_width=" wrap_content "android:layout_height=" Wrap_content " Android:textsize= "18DP" android:text= "Please choose your favorite fruit:"/> <spinner android:layout_width= "Match_par Ent "android:layout_height=" wrap_content "android:id=" @+id/spinner "/> </LinearLayout> &l T 
    TextView android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:textsize= "18DP" Android:id= "@+id/tv"/> 
 
</LinearLayout>  

Spinner is the component of the Drop-down list

1.2.mainactivity.class

public class Mainactivity extends Appcompatactivity {private Spinner s; 
  String[] data=new string[]{"Apple", "Sydney", "watermelon", "Grape", "orange", "Strawberry"}; 
 
  Private TextView TV; 
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
 
    Setcontentview (R.layout.activity_main); 
    tv= (TextView) This.findviewbyid (r.id.tv); 
    S= (Spinner) This.findviewbyid (R.id.spinner); S.setadapter (New arrayadapter<string> (this,android). 
    R.layout.simple_list_item_1,data)); S.setonitemselectedlistener (New Adapterview.onitemselectedlistener () {@Override public void onitemselected ( 
        Adapterview<?> Parent, view view, int position, long id) {String str=data[position]; 
      Tv.settext ("Favorite fruit is:" +str); 
  @Override public void onnothingselected (adapterview<?> parent) {}}); } s.setadapter (New Arrayadapter<string> (this,android). R.layout.simple_list_item_1,data));Ndroid. R.layout.simple_list_item_1 refers to Android's own Drop-down list format, where data is the source; S.setonitemselectedlistener (new Adapterview.onitemselectedlistener ()) {..} ; Is the monitor of the dropdown list

Effect Chart:

The above is the entire content of this article, I hope to learn how to master the Android implementation of the Pull-down menu spinner components help.

Related Article

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.