Examples of custom spinner for Android development

Source: Internet
Author: User


Recently in the project has a lot of drop-down box, in order to realize the convenience of using the Android spinner, but the spinner style does not meet the requirements, and then learn the custom spinner. Here's the whole step:

1. Get the picture ready.

Defined in 2.style

<!--spinner-->
<style name= "Spinner_style" >
<item name= "Android:background" > @drawable/spinner</item>
<item name= "Android:paddingleft" >5dip</item>

3. Call

<spinner
Android:id= "@+id/field_item_spinner_content"
style= "@style/spinner_style"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"/>


4. Define Simple_spinner_item.xml in Layout

<?xml version= "1.0" encoding= "Utf-8"?>
<checkedtextview xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:id= "@android: Id/text1"
android:paddingleft= "5dip"
android:paddingright= "5dip"
Android:gravity= "Center_vertical"
Android:textcolor= "#808080"
Android:singleline= "true"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"/>

5.java Code

arrayadapter<string> adapter = new Arrayadapter<string> (Mcontext,r.layout.simple_spinner_item);
String level[] = getresources (). Getstringarray (R.array.affair_level);//resource file
for (int i = 0; i < level.length; i++) {
Adapter.add (Level[i]);
}
Adapter.setdropdownviewresource (Android. R.layout.simple_spinner_dropdown_item);
Spinner.setadapter (adapter);

Effect Chart:

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.