The use of the Android Spinner pull-down menu _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

Steps:

1. Define spinner control

Copy Code code 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 point to a string-->

2. Create a new Android XML file named arrays under the Res/values/folder
Copy Code code 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 contents of the file are automatically added to the R file, referenced by: r.array.planets; This is the list of user Drop-down selections;

3. Add the following code to the OnCreate method of the activity

Copy Code code 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!

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.