Android旋转器每行的背景不同 [英] Android Spinner different background for each row

查看:288
本文介绍了Android旋转器每行的背景不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个主题已经解决了很多次,我发现了几个问题,但我不能满足我的需要。我想在旋转器中有一个颜色列表。我这样做,但我的微调框是空的。
在我的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 =bluetype =color>#FF33B5E5< / item>
< item name =purpletype =color>#FFAA66CC< / item>
< item name =greentype =color>#FF99CC00< / item>
< item name =orangetype =color>#FFFFBB33< / item>
< item name =redtype =color>#FFFF4444< / item>
< item name =darkbluetype =color>#FF0099CC< / item>
< item name =darkpurpletype =color>#FF9933CC< / item>
< item name =darkgreentype =color>#FF669900< / item>
< item name =darkorangetype =color>#FFFF8800< / item>
< item name =darkredtype =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>

解决方案>

这很容易,你必须



1.为自定义转换器编写自定义适配器,这里是如何做的

  class SpinnerAdapter extends BaseAdapter 
{
ArrayList< Integer>颜色;
上下文上下文;

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
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;
}

}

  spnColors =(Spinner)findViewById(R.id.spnColor); 
spnColors.setAdapter(new SpinnerAdapter(this));

最终结果是





请接受回答,如果有帮助! p>

I know that this topic has been addressed many times, I found several questions like this but I can not fit my need. I want to have a list of colors in a spinner. I did so, but my spinner is empty. in my 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); 

and In folder values I created a file 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>

解决方案

It's easy , you have to

1.Write your own custom adapter for spinner, here is how you do it

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.Set the adapter like this

spnColors=(Spinner)findViewById(R.id.spnColor);
spnColors.setAdapter(new SpinnerAdapter(this));

Final Result is

Do Accept Answer, If it helps!

这篇关于Android旋转器每行的背景不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆