Android Spinner每行的背景不同

时间:2014-04-29 16:36:40

标签: java android arrays colors spinner

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

在我的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); 

和In文件夹值我创建了一个文件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> 

3 个答案:

答案 0 :(得分:11)

这很容易,你必须

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

最终结果

color spinner

接受答案,如果有帮助的话!

答案 1 :(得分:0)

您可能希望展示更多代码,很难准确地说明3行代码的用途:)

尝试将Integer数组移动到其值文件夹中名为array.xml的文件中。

<?xml version="1.0" encoding="utf-8"?> <resources> <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>

答案 2 :(得分:0)

如果我理解正确,你想为每个Spinner项目显示颜色本身?您可以通过创建自定义适配器并覆盖getDropDownView()来轻松完成此操作。

举个例子:

public class CustomSpinnerAdapter extends ArrayAdapter<CharSequence>
{
    private List<Integer> mColors;

    public CustomSpinnerAdapter(Context context, List<CharSequence> items, List<Integer> colors)
    {
        super(context, android.R.layout.simple_spinner_item, items);
        mColors = colors;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent)
    {
        View view = super.getDropDownView(position, convertView, parent);
        applyColor(view, position);
        return view;
    }

    private void applyColor(View view, int position)
    {
        view.setBackgroundColor(mColors.get(position));

        // You can change the TextView if you want (in addition to or instead of the background).
        TextView text = (TextView)view.findViewById(android.R.id.text1);
        text.setTextColor(mColors.get(position));
    }
}

当然,您也可以创建自定义视图并将其返回。