Customize the TintSpinner style and tintspinner Style

Source: Internet
Author: User

Customize the TintSpinner style and tintspinner Style

First, why should we use TintSpinner instead of a Spinner?

See several compatibility issues when using AppCompat_v7 21.0.0d

Then, the effect we want to customize is



That is to say, the selected style is white text, and the drop-down style is black text. Such a small need also requires skill.

First, the TintSpinner adapter needs to use the ArrayAdapter instead of the BaseAdapter. The previous detour was to use the BaseAdapter and then find

adapter.setDropDownViewResource(R.layout.drop_down_item);

Method, the result is obviously easy to see, BaseAdapter does not have this method, why?


BaseAdapter implements the ListAdapter and SpinnerAdapter interfaces:

Public abstract class BaseAdapter implements ListAdapter, SpinnerAdapter

The ArrayAdapter is a subclass of BaseAdapter.

Public class ArrayAdapter <T> extends BaseAdapter implements Filterable

Therefore, the setDropDownViewResource method cannot be found.


The problem is solved. Let's see how to implement this adapter:

Model is

public class EndemicArea {    @JsonField("description")    private String description;    @JsonField("name")    private String name;    @JsonField("pk")    private int pk;    public EndemicArea(String description, String name, int pk) {        this.description = description;        this.name = name;        this.pk = pk;    }    public String getDescription() {        return description;    }    public void setDescription(String description) {        this.description = description;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getPk() {        return pk;    }    public void setPk(int pk) {        this.pk = pk;    }}

Adapter:

public class EndemicAreaSpinnerAdapter extends ArrayAdapter<EndemicArea> {    private LayoutInflater layoutInflater;    public EndemicAreaSpinnerAdapter(Context context, int resource, List<EndemicArea> endemicAreaList) {        super(context, resource, endemicAreaList);        layoutInflater = LayoutInflater.from(context);    }    @Override    public View getView(int position, View convertView, ViewGroup parent) {        View view = layoutInflater.inflate(R.layout.choose_bed_item, null);        TextView areaName = (TextView) view.findViewById(R.id.choose_bed_item_name);        areaName.setText(getItem(position).getName());        return view;    }    @Override    public View getDropDownView(int position, View convertView, ViewGroup parent) {        View view = layoutInflater.inflate(android.R.layout.simple_spinner_dropdown_item, parent, false);        CheckedTextView areaName = (CheckedTextView) view.findViewById(android.R.id.text1);        areaName.setText(getItem(position).getName());        return view;    }}

Choose_bed_item.xml is

<?xml version="1.0" encoding="utf-8"?><TextView xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/choose_bed_item_name"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_margin="4dp"    android:gravity="center"    android:paddingBottom="10dp"    android:layout_marginRight="8dp"    android:paddingTop="10dp"    android:textColor="@color/white"    android:textSize="20sp"></TextView>

Finally:

 tintSpinner = new TintSpinner(getActivity());        tintSpinner.setBackgroundResource(R.drawable.abc_spinner_mtrl_am_alpha);        tintSpinner.setAdapter(spinnerAdapter);

Note: TintSpinner is used to import android. support. v7.internal. widget. TintSpinner;

It seems that the Spinner needs ArrayAdapter to customize the drop-down style, and then it can be completely customized.




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.