Android getting started tutorial (13th)-custom drop-down menu mode-Application of spinner and setdropdownviewresource)

Source: Internet
Author: User
Hello everyone, let's take a look at the drop-down menu spinner under android in this section, just like the swing ComboBox and HTML <SELECT>. Due to limited mobile phone images, to select a project within a limited range, the drop-down menu is a good choice.

 

The drop-down menu of the spinner widget provided by Android is very easy to use and the style is also applicable. however, the focus of the demo in this section is to customize the style in the drop-down menu. The key is to call the setdropdownviewresource method to define the display style of the drop-down menu in XML format.

 

Step 1:Create a newAndroidEngineering,We name itSpinnerdemo.

Step 2: OpenLayoutFolder, findMain. xmlChange the content:

<? XML version = "1.0" encoding = "UTF-8"?>

<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"

Android: Orientation = "vertical"

Android: padding = "10dip"


Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content">

<Textview

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

Android: layout_margintop = "10dip"

Android: text = "Please select a planet :"



/>

<Spinner

Android: Id = "@ + ID/Spinner"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

Android: drawselectid Top = "true"

Android: prompt = "@ string/planet_prompt"


/>



</Linearlayout>

Note:


The spinner's Android: Prompt



Is a string resource. In this case, Android does not allow it to be a string, it must

Be a reference to a resource. So...



Step 3
: Find Res/values/string. XML,
Add the following ( Simhei
) Line:



<? XML version = "1.0" encoding = "UTF-8"?>

<Resources>

<String name = "hello"> Hello world, spinnerdemo! </String>

<String name = "app_name"> spinnerdemo </string>

<String name = "planet_prompt"> choose a planet </string>


</Resources>



Step 4
: In Res/values/
Create a folder XML
File named Arrays. xml:




<Resources>


<String-array name = "planets">


<Item> mercury </item>

<Item> Venus </item>

<Item> Earth </item>

<Item> Mars </item>

<Item> Jupiter </item>

<Item> Saturn </item>

<Item> Uranus </item>

<Item> Neptune </item>

</String-array>


</Resources>


This is the option that you can select from the spinner widget list.



Step 5
: Open Spinnerdemo. Java
, The edit content is as follows:



Package com. Android. test;

Import Android. App. activity;

Import Android. OS. Bundle;

Import Android. widget. arrayadapter;

Import Android. widget. spinner;



Public class Spinnerdemo
Extends activity {

Public void oncreate (bundle savedinstancestate ){

Super. oncreate (savedinstancestate );

Setcontentview (R. layout. Main );

Spinner S = (spinner) findviewbyid (R. Id. spinner );

Arrayadapter adapter = arrayadapter. createfromresource (

This, R. array. Planets, Android. R. layout. simple_spinner_item)



Adapter. setdropdownviewresource (Android. R. layout. simple_spinner_dropdown_item );

S. setadapter (adapter );



}

}



Step 6:
Last Run it
(Running) the effect is as follows:





After a long wait, we finally finished Japanese... android, English, I'm coming !!!!

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.