AppCompat Learning (1)-appcompatspinner

Source: Internet
Author: User

There are two spinner controls in Andriod, one is its own spinner and the other is Android.support.v7.widget.AppCompatSpinner.

The difference is that the V7 within the spinner is compatible with the low version, spinner later version in order to use the method of V7 spinner can be compatible to 2.1 (V7 compatible to Api7), the first two of the use of no other differences, recommend the use of V7, Guaranteed effects can be displayed on different versions.

Spinner Use the following steps:

1. Setting up a data source

Create the following file under the Values folder:

Values/arrays.xml

<!--? xml version= "1.0" encoding= "Utf-8"? -<Resources>    <String-arrayname= "Week">        <Item>Monday</Item>        <Item>Tuesday</Item>        <Item>Wednesday</Item>        <Item>Thursday</Item>        <Item>Friday</Item>        <Item>Saturday</Item>        <Item>Sunday</Item>    </String-array></Resources>

Layout/login.xml

< Android.support.v7.widget.AppCompatSpinner                 Android:id = "@+id/sp_select_email"                 android:layout_width= "Wrap_content"                android:layout_height= "Wrap_ Content "                android:entries=" @array/login_company_email "                android: Spinnermode= "dropdown"                android:theme= "@style/common_spinner"                />
Android:entries//Incoming data in Arrayx.xml in the Values folder android:spinnerMode  //显示模式有popmenu和dialog两种 android:prompt //当显示模式为dialog时生效,作用为显示dialog的标题内容

There are some other common properties:

2. Set the display theme
< Android.support.v7.widget.AppCompatSpinner                 Android:id = "@+id/sp_select_email"                 android:layout_width= "Wrap_content"                android:layout_height= "Wrap_ Content "                android:entries=" @array/login_company_email "                android: Spinnermode= "dropdown"                android:theme= "@style/common_spinner"                />

@style/common_spinner is setting the subject of the drop-down item.

Styles.xml

<stylename= "Common_spinner"Parent= "Widget.AppCompat.DropDownItem.Spinner">        <Itemname= "Android:textcolor">@color/app_text_color</Item>        <Itemname= "Android:textsize">14sp</Item>    </style>

3. Add Response Events

Spinner Response event is Onitemselectedlistener , do not write Onitemclicklistener, otherwise direct error:

 Mspinner.setonitemselectedlistener (new   Adapterview.onitemselectedlistener () {@Override  public  " Span style= "COLOR: #0000ff" >void  onitemselected (adapterview<!--?--> parent, view view, int  position, long   ID) {              //  Select action for list item   @Override  public  void  onnothingselected (adapterview<!--?--> parent) {            Span style= "COLOR: #008000" >//  action not selected   }        });

You can also use code to add a data source to spinner, where you only need to include the basic width-height attribute in the layout file.

Selection of adapters:

The most convenient adapter is arrayadapter, with the disadvantage that only a single TextView content (with List storage data) can be displayed

New// passed parameters are context, TextView of unselected items, data source list// set dropdown textview Arrayadapter.setdropdownviewresource (R.layout.item_drop);

R.layou.item_select

<!--? xml version= "1.0" encoding= "Utf-8"? -<TextViewxmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
Android:textsize= "20DP"
Android:typeface= "Serif"
Android:gravity= "Center"
Android:textcolor= "#000000"
Android:layout_height= "Match_parent"
Android:background= "@color/coloraccent"
Android:orientation= "vertical"> </TextView>

R.layout.item_drop

<!--? xml version= "1.0" encoding= "Utf-8"? -<TextViewxmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
Android:layout_height= "Match_parent"
Android:padding= "5DP"
Android:background= "@color/colorprimary"
Android:gravity= "Center"
Android:orientation= "vertical"
Android:textcolor= "#000000"
Android:textsize= "20DP"> </TextView>

The effect is as follows:

Sometimes the actual requirements may require that the spinner drop-down is not just a textview, then the adapter needs to be customized

Custom adapter:

Private classMyadapterImplementsSpinneradapter {PrivateThemedspinneradapter.helper Helper; @Override Public voidRegisterdatasetobserver (Datasetobserver observer) {} @Override Public voidUnregisterdatasetobserver (Datasetobserver observer) {} @Override Public intGetCount () {returnmlist.size (); } @Override PublicObject GetItem (intposition) {            returnMlist.get (position); } @Override Public LongGetitemid (intposition) {            returnposition; } @Override Public BooleanHasstableids () {return false; } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {TextView TextView=NewTextView (Mcontext); Textview.settextsize (30);            Textview.setbackgroundcolor (Getresources (). GetColor (r.color.coloraccent));            Textview.settext (Mlist.get (position)); returnTextView; } @Override Public intGetitemviewtype (intposition) {            return1; } @Override Public intGetviewtypecount () {return1; } @Override Public BooleanIsEmpty () {return false; } @Override PublicView Getdropdownview (intposition, View Convertview, ViewGroup parent) {TextView TextView=NewTextView (Mcontext); Textview.settextsize (30);            Textview.setbackgroundcolor (Getresources (). GetColor (r.color.colorprimary));            Textview.settext (Mlist.get (position)); returnTextView; }    }

The effect is as follows (the color is set for the drop-down and default display as a distinction, and a drop-down offset is not used for ease of referencing the layout file):

Now that the adapter has been used, the same layout can be implemented by default display and drop-down, but also different layouts that basically meet the requirements

There are also several new methods in 23 that can do more customization (using Appcomptspinner to fit into the lower version):

Mspinner.setpopupbackgroundresource (R.drawable.back); Mspinner.setbackgroundresource (R.drawable.back);
Set the background for the default display and drop-down menus  

AppCompat Learning (1)-appcompatspinner

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.