java–Android Spinner每行的背景不同

java–Android Spinner每行的背景不同,第1张

概述我知道这个话题已被多次解决,我发现了几个这样的问题,但我不能满足我的需要.我希望在微调器中有一个颜色列表.我这样做了,但我的旋转器是空的.在我的OnCreate()中:spinner=(Spinner)findViewById(R.id.spinner1);ArrayAdapter<CharSequence>adapter=ArrayAdapter.createF

我知道这个话题已被多次解决,我发现了几个这样的问题,但我不能满足我的需要.我希望在微调器中有一个颜色列表.我这样做了,但我的旋转器是空的.

在我的OnCreate()中:

spinner = (Spinner) findVIEwByID(R.ID.spinner1); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromresource(    this, R.array.androIDcolors, androID.R.layout.simple_spinner_item);adapter.setDropDownVIEwResource(    androID.R.layout.simple_spinner_dropdown_item);spinner.setAdapter(adapter); 

在文件夹值中,我创建了一个文件colors.xml:

<resources><item name="blue" type="color">#FF33B5E5</item><item name="purple" type="color">#FFAA66CC</item><item name="green" type="color">#FF99CC00</item><item name="orange" type="color">#FFFFBB33</item><item name="red" type="color">#FFFF4444</item><item name="darkblue" type="color">#FF0099CC</item><item name="darkpurple" type="color">#FF9933CC</item><item name="darkgreen" type="color">#FF669900</item><item name="darkorange" type="color">#FFFF8800</item><item name="darkred" type="color">#FFCC0000</item><integer-array name="androIDcolors">    <item>@color/blue</item>    <item>@color/purple</item>    <item>@color/green</item>    <item>@color/orange</item>    <item>@color/red</item>    <item>@color/darkblue</item>    <item>@color/darkpurple</item>    <item>@color/darkgreen</item>    <item>@color/darkorange</item>    <item>@color/darkred</item></integer-array></resources> 

解决方法:

这很简单,你必须这样做

1.为微调器编写自己的自定义适配器,这是你如何做到的

class SpinnerAdapter extends BaseAdapter{    ArrayList<Integer> colors;    Context context;    public SpinnerAdapter(Context context)     {        this.context=context;        colors=new ArrayList<Integer>();        int retrIEve []=context.getResources().getIntArray(R.array.androIDcolors);        for(int re:retrIEve)        {            colors.add(re);        }    }    @OverrIDe    public int getCount()     {        return colors.size();    }    @OverrIDe    public Object getItem(int arg0)     {            return colors.get(arg0);    }    @OverrIDe    public long getItemID(int arg0)     {        return arg0;    }    @OverrIDe    public VIEw getVIEw(int pos, VIEw vIEw, VIEwGroup parent)     {        LayoutInflater inflater=LayoutInflater.from(context);        vIEw=inflater.inflate(androID.R.layout.simple_spinner_dropdown_item, null);        TextVIEw txv=(TextVIEw)vIEw.findVIEwByID(androID.R.ID.text1);        txv.setBackgroundcolor(colors.get(pos));        txv.setTextSize(20f);        txv.setText("Text  "+pos);        return vIEw;    }}

2.像这样设置适配器

spncolors=(Spinner)findVIEwByID(R.ID.spncolor);spncolors.setAdapter(new SpinnerAdapter(this));

最终结果是

请接受答案,如果有帮助!

总结

以上是内存溢出为你收集整理的java – Android Spinner每行的背景不同全部内容,希望文章能够帮你解决java – Android Spinner每行的背景不同所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1116436.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存