Use arrayadapter to create a spinner (custom layout, default layout, dynamic content, and static content)

Source: Internet
Author: User

Android: dropdownwidth drop-down list width
Android: dropdownhorizontaloffset
Android: vertical offset between the dropdownverticaloffset drop-down menu and text
Android: popupbackground background color in the drop-down list

Android: prompt information in the prompt drop-down list (basically useless)

Mainactivity. Java

Package COM. kale. spinner; import android. app. activity; import android. OS. bundle; import android. view. view; import android. widget. adapterview; import android. widget. adapterview. onitemselectedlistener; import android. widget. arrayadapter; import android. widget. spinner; import android. widget. toast; public class mainactivity extends activity {fig 01, fig 02, fig 03; @ override protected void oncret Ate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); viewinit (); string [] arr = {"Java", "android", "XML", "iOS "}; // use the layout provided by Android to create arrayadapter <string> adapter01 = new arrayadapter <> (this, android. r. layout. simple_list_item_multiple_choice, arr); spinner01.setadapter (adapter01); // listener spinner01.setonitemselectedlistener (New onitemselectedliste NER () {@ override public void onitemselected (adapterview <?> Adapterview, view arg1, int position, long ID) {// todo automatically generated method stub string selected = adapterview. getitematposition (position ). tostring (); toast. maketext (getapplicationcontext (), selected, 0 ). show () ;}@ override public void onnothingselected (adapterview <?> Arg0) {}}); // ---------------------------------------------------------------------------- // The default layout file sets the spinner style and is created using the written data. Not recommended arrayadapter <charsequence> adapter02 = arrayadapter. createfromresource (this, R. array. item, android. r. layout. simple_spinner_item); // set the style adapter02.setdropdownviewresource (Android. r. layout. simple_spinner_dropdown_item); spinner02.setadapter (adapter02); // spinner02.setprompt ("test"); // set the prompt text, which is no longer displayed on the current interface. So it can be said that no dynamic data is used. The layout file is the custom item arrayadapter <string> adapter03 = new arrayadapter <> (this, R. layout. item, R. id. textview_id, arr); spinner03.setadapter (adapter03);} private void viewinit () {spinner01 = (spinner) findviewbyid (R. id. spinner01_id); spinner02 = (spinner) findviewbyid (R. id. spinner02_id); spinner03 = (spinner) findviewbyid (R. id. spinner03_id );}}

 

Activity_main.xml

<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" Android: padding = "16dp"> <! -- Android: dropdownwidth drop-down list width Android: dropdownhorizontaloffset the distance from the drop-down list to the left Android: dropdownverticaloffset the vertical offset between the drop-down menu and text Android: Background Color Android in the popupbackground drop-down list: prompt information in the prompt drop-down list --> <textview Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: TEXT = "use code to fill in the data's spinner (use the layout provided by Android)" Android: layout_margintop = "16dp" Android: textappearance = "? Android: ATTR/textappearancemedium "/> <spinner Android: Id =" @ + ID/spinner01_id "Android: layout_width =" match_parent "Android: layout_height =" wrap_content "Android: popupbackground = "# ade9ee" Android: dropdownverticaloffset = "3dp" Android: prompt = "@ string/hello_world"/> <textview Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "data is written to the spinner in the array (layout provided by Android)" Android: Layout_margintop = "26dp" Android: textappearance = "? Android: ATTR/textappearancemedium "/> <spinner Android: Id =" @ + ID/spinner02_id "Android: layout_width =" match_parent "Android: layout_height =" wrap_content "Android: layout_margintop = "16dp" Android: dropdownverticaloffset = "3dp" Android: prompt = "@ string/hello_world"/> <textview Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "Use a custom layout and dynamically enter the data in the spinner" Android: layout_margin Top = "26dp" Android: textappearance = "? Android: ATTR/textappearancemedium "/> <spinner Android: Id =" @ + ID/spinner03_id "Android: layout_width =" match_parent "Android: layout_height = "wrap_content"/> </linearlayout>

 

Item. xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"     android:padding="8dp">    <TextView        android:id="@+id/textView_id"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Large Text"        android:textColor="#0000ff"        android:gravity="center_horizontal"        android:textAppearance="?android:attr/textAppearanceLarge" /></LinearLayout>

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.