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 newAndroid
Engineering,
We name itSpinnerdemo
.
Step 2
: OpenLayout
Folder, findMain. xml
Change 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'sandroid: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
: FindRes/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
: InRes/values/
Create a folderXML
File namedArrays. 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
: OpenSpinnerdemo. 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 classSpinnerdemo
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:
LastRun it
(Running) the effect is as follows:
After a long wait, we finally finished Japanese... android, English, I'm coming !!!!