The latest spinner usage detailed

Source: Internet
Author: User
Tags getcolor

The latest spinner usage detailed

SpinnerA total of two components, one is its own Spinner, one is android.support.v7.widget.AppCompatSpinnerThe difference between the two is v7Within the Spinneris compatible with the lower version, SpinnerA later version of the method can be used in exchange for v7Under the SpinnerCan always be compatible to 2.1( v7Compatible to api7), there is no other difference between the use of the first and the other, it is recommended v7To ensure that the effects are displayed on different versions. SpinnerSteps to use:

The layout of the data source and theme, characterized by convenient and fast, the disadvantage is not to dynamically change the data to be displayed, the use of adapter features are flexible, for the project needs to choose. xmlSet up data sources and themes in data sources
//使用entries属性,传入的是values文件夹下的arrays.xml内的数据 <android.support.v7.widget.AppCompatSpinner            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:entries="@array/week">
Values/arrays.xml file
<?xml version= "1.0" encoding= "Utf-8"?><resources>    <string-array name="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>
After the above operation Spinnercan be used (after clicking the event and then adding), the effect is as follows: Here is the common XML attributes to paste:

Are better understood, explain several of these properties:
android:spinnerMode  //显示模式 :popu和dialog两种//当显示模式为dialog时生效,作用为显示dialog的标题内容

Show settings for a theme
app:theme="@style/myDropTheme"  //设置的是下拉item的主题 ,但是会覆盖未下拉的显示//所以如果不想再代码中设置主题的话最好主题设置成白色背景的,效果比较好
Style.xml-mydroptheme
name="myDropTheme" parent="Widget.AppCompat.DropDownItem.Spinner">        <itemname="android:textColor">@color/colorAccent</item>        <itemname="android:textSize">20sp</item>        <itemname="android:background">@color/colorPrimary</item>    </style>

As follows:

To add a response event: SpinnerThe response event is OnItemSelectedListener, never write onItemClickListener, or direct error:
 mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {            @Override            publicvoidonItemSelectedintlong id) {                //选择列表项的操作            }            @Override            publicvoidonNothingSelected(AdapterView<?> parent) {                //未选中时候的操作            }        });
Set in code SpinnerThe layout file only needs to contain basic high-width properties to choose the adapter: The most convenient adapter is ArrayAdapter, the disadvantage is that you can only display a single TextViewContent (with ListStore data)
 ArrayAdapter arrayAdapter = new ArrayAdapter(mContext, R.layout.item_select, mList); //传入的参数分别为 Context , 未选中项的textview , 数据源List//单独设置下拉的textviewarrayAdapter.setDropDownViewResource(R.layout.item_drop);
R.layout.item_select
<?xml version= "1.0" encoding= "Utf-8"?><TextView xmlns: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"?><TextView xmlns: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 (the color is set to differentiate:

Custom AdapterThe actual requirements may require SpinnerDrop-down does not just show a TextView, you will need to customize the adapter at this time
Private  class myadapter implements spinneradapter {        PrivateThemedspinneradapter.helper Helper;@Override         Public void Registerdatasetobserver(Datasetobserver observer) {        }@Override         Public void Unregisterdatasetobserver(Datasetobserver observer) {        }@Override         Public int GetCount() {returnMlist.size (); }@Override         PublicObjectGetItem(intPosition) {returnMlist.get (position); }@Override         Public Long Getitemid(intPosition) {returnPosition }@Override         Public Boolean Hasstableids() {return false; }@Override         PublicViewGetView(intPosition, View Convertview, ViewGroup parent) {TextView TextView =NewTextView (Mcontext); Textview.settextsize ( -);            Textview.setbackgroundcolor (Getresources (). GetColor (r.color.coloraccent)); Textview.settext (Mlist.get (position));returnTextView; }@Override         Public int Getitemviewtype(intPosition) {return 1; }@Override         Public int Getviewtypecount() {return 1; }@Override         Public Boolean IsEmpty() {return false; }@Override         PublicViewGetdropdownview(intPosition, View Convertview, ViewGroup parent) {TextView TextView =NewTextView (Mcontext); Textview.settextsize ( -);            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):

At this point the use of the adapter is completed, you can achieve the default display and drop-down using the same layout, but also different layouts, basically meet the requirements of the other there are several 23In the new method, you can do more customization (use Appcomptspinner to fit into the lower version):
mSpinner.setPopupBackgroundResource(R.drawable.back);mSpinner.setBackgroundResource(R.drawable.back);//给默认显示项和下拉菜单设置背景      
Simple Add a background

Complete

The latest spinner usage detailed

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.