spinner自定义边框样式

本文介绍如何自定义Spinner的边框样式,通过创建自定义Adapter,包括实体类、item布局文件和继承BaseAdapter。同时,重写ArrayAdapter的getDropDownView()方法来实现下拉列表的显示,特别指出必须使用CheckedTextView以避免错误。
摘要由CSDN通过智能技术生成

一、自定义adapter

1.创建实体类

public class SpinnerName {
    private String name;

    public SpinnerName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "SpinnerName{" +
                "name='" + name + '\'' +
                '}';
    }
}

2.定义每个item的布局文件

<?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:background="@drawable/inputbox"
    android:orientation="vertical">

    <TextView
        android:id="@+id/style_spinner_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:background="@drawable/inputbox" />
</LinearLayout>

3.自定义adapter继承BaseAdapter

public class SpinnerAdapter extends BaseAdapter {
    private Context context;
    private List<SpinnerName> data;

    public SpinnerAdapter(Context context, List<SpinnerName> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder;
        if (convertView == null) {
            holder = new Holder();
            convertView = LayoutInflater.from(context).inflate(R.layout.style_spinner, parent, false);
            holder.txt = convertView.findViewById(R.id.style_spinner_txt);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        holder.txt.setText(data.get(position).getName());
        return convertView;
    }

    class Holder {
        TextView txt;
    }
}

4.在activity中加载adapter

        List<SpinnerName> list = new ArrayList<SpinnerName>();
        list.add(new SpinnerName("aaa"));
        list.add(new SpinnerName("bbb"));
        list.add(new SpinnerName("ccc"));
        list.add(new SpinnerName("ddd"));
        SpinnerAdapter adapter = new SpinnerAdapter(ConfigNetActivity.this, list);
        spin.setAdapter(adapter);

二、重写ArrayAdapter的getDropDownView()方法来实现

1.activity中代码

final List<String> list = new ArrayList<>();
        list.add("aaa");
        list.add("bbb");
        list.add("ccc");
        list.add("ddd");
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(ConfigNetActivity.this,
                R.layout.spinner_checked_text, list) {

            @Override
            public View getDropDownView(int position, View convertView,
                                        ViewGroup parent) {
                View view = inflate(getContext(), R.layout.spinner_item_layout,
                        null);
                TextView label = (TextView) view
                        .findViewById(R.id.spinner_item_label);
                label.setText(list.get(position));
                if (spin.getSelectedItemPosition() == position) {
                    view.setBackgroundColor(getResources().getColor(
                            R.color.spinner_green));
                } else {
                    view.setBackgroundColor(getResources().getColor(
                            R.color.spinner_light_green));
                }

                return view;
            }

        };

        adapter.setDropDownViewResource(R.layout.spinner_item_layout);
        spin.setAdapter(adapter);

2.spinner_checked_text.xml的代码(注意:必须是CheckedTextView,否则会出错。

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="5dp"
    android:gravity="center_vertical"
    android:singleLine="true"
    android:textColor="#666666"
    android:textSize="18sp">

</CheckedTextView>

3.第二部分就是那个弹出来的下拉窗口,这个是重写ArrayAdapter的getDropDownView()方法来实现的:

final List<String> list = new ArrayList<>();
        list.add("aaa");
        list.add("bbb");
        list.add("ccc");
        list.add("ddd");
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(ConfigNetActivity.this,
                R.layout.spinner_checked_text, list) {

            @Override
            public View getDropDownView(int position, View convertView,
                                        ViewGroup parent) {
                View view = inflate(getContext(), R.layout.spinner_item_layout,
                        null);
                TextView label = (TextView) view
                        .findViewById(R.id.spinner_item_label);
                label.setText(list.get(position));
                if (spin.getSelectedItemPosition() == position) {
                    view.setBackgroundColor(getResources().getColor(
                            R.color.spinner_green));
                } else {
                    view.setBackgroundColor(getResources().getColor(
                            R.color.spinner_light_green));
                }
                return view;
            }

        };
        adapter.setDropDownViewResource(R.layout.spinner_item_layout);
        spin.setAdapter(adapter);

 其中spinner_item_layout.xml的代码如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/spinner_light_green"
    android:padding="5dp">

    <TextView
        android:id="@+id/spinner_item_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:textColor="@color/text_green"
        android:textSize="17sp" />

</RelativeLayout>
颜色示例:
 
<color name="text_green">#48713a</color>
<color name="spinner_light_green">#e4ead3</color>
<color name="spinner_green">#90ac58</color>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值