个别微调旋转器项目的背景颜色

huangapple go评论80阅读模式
英文:

Individual backround color for spinner items

问题

Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
            R.array.duesentypen, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

String[] hex = {"#886da8", "#ffc3cf", "#ef9b00", "#2bb430", "#f7dc01", "#bc65a2", "#487ebf", "#e00124", "#b36634", "#949494", "#ffffff", "#000000", "#4bbbd0", "#8b6d9f", "#8ccff4"};
// 第一个 spinner 项的颜色:hex[0]
// 第二个 spinner 项的颜色:hex[1]
英文:

I would like to color each item´s backround of the spinner individual.
The items are set with this code:

Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter&lt;CharSequence&gt; adapter = ArrayAdapter.createFromResource(this,
            R.array.duesentypen, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

The hex values are stored in an array. Whereas the index count refers to the item count. E.g.:

String[] hex = {&quot;#886da8&quot;, &quot;#ffc3cf&quot;,&quot;#ef9b00&quot;, &quot;#2bb430&quot;, &quot;#f7dc01&quot;, &quot;#bc65a2&quot;, &quot;#487ebf&quot;,&quot;#e00124&quot;,&quot;#b36634&quot;,&quot;#949494&quot;, &quot;#ffffff&quot;,&quot;#000000&quot;,&quot;#4bbbd0&quot;, &quot;#8b6d9f&quot;, &quot;#8ccff4&quot;};
//color for the first spinner item: hex[0]
//color for the second spinner item: hex[1]

How could I do that in the best way?

答案1

得分: 0

final List<String> plantsList = new ArrayList<>(Arrays.asList(plants));

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
    R.array.duesentypen, android.R.layout.simple_spinner_item) {
    @Override
    public View getDropDownView(int position, View convertView,
                                ViewGroup parent) {
        View view = super.getDropDownView(position, convertView, parent);
        TextView tv = (TextView) view;

        switch(position) {
            case 0:
                tv.setTextColor(Color.parseColor("#FF7C7967"));
            case 1:
                tv.setTextColor(Color.parseColor("#FF7C7967"));
        }

        return view;
    }
};

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
英文:

Try to change color according to Position.

final List&lt;String&gt; plantsList = new ArrayList&lt;&gt;(Arrays.asList(plants));

        ArrayAdapter&lt;CharSequence&gt; adapter = ArrayAdapter.createFromResource(this,
            R.array.duesentypen, android.R.layout.simple_spinner_item){
            @Override
            public View getDropDownView(int position, View convertView,
                                        ViewGroup parent) {
                View view = super.getDropDownView(position, convertView, parent);
                TextView tv = (TextView) view;
               
        switch(position) {
             case 0:
                tv.setTextColor(Color.parseColor(&quot;#FF7C7967&quot;));
             case 1:
                tv.setTextColor(Color.parseColor(&quot;#FF7C7967&quot;));
                   }
                
                return view;
            }
        };

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);

答案2

得分: 0

public class ColorAdapter extends BaseAdapter {

    private ArrayList<Integer> mColors;
    private String[] mItems;

    ColorAdapter2(String[] items) {
        mItems = items;
        String[] hex = {"#886da8", "#ffc3cf", "#ef9b00", "#2bb430", "#f7dc01", "#bc65a2", "#487ebf", "#e00124", "#b36634", "#949494", "#ffffff", "#000000", "#4bbbd0", "#8b6d9f", "#8ccff4"};

        mColors = new ArrayList<>();
        for (String color : hex) {
            mColors.add(Color.parseColor(color));
        }
    }

    @Override
    public int getCount() {
        return mItems.length;
    }

    @Override
    public Object getItem(int args) {
        return mColors.get(args);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;

        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            convertView = inflater.inflate(android.R.layout.simple_spinner_dropdown_item, null);
            holder = new ViewHolder();
            holder.textView = convertView.findViewById(android.R.id.text1);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.textView.setBackgroundColor(mColors.get(position % mColors.size()));
        holder.textView.setText(mItems[position]);
        return convertView;
    }

    /* Return spinner text of the item at particular position*/
    public String getItemAtPosition(int position) {
        if (position > -1 && position < mItems.length)
            return mItems[position];
        else
            return null;
    }

    static class ViewHolder {
        TextView textView;
    }
}
Spinner spinner = findViewById(R.id.spinner);
spinner.setAdapter(new ColorAdapter(getResources().getStringArray(R.array.myItems)));
<resources>
    <string name="app_name">Color Activity</string>
    <string-array name="myItems">
        <item>item1</item>
        <item>item2</item>
        <item>item3</item>
        <item>item4</item>
        <item>item5</item>
    </string-array>
</resources>
String item = ((ColorAdapter) spinner.getAdapter()).getItemAtPosition(position);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        Log.d(TAG, "onItemSelected: " + ((ColorAdapter) spinner.getAdapter()).getItemAtPosition(position));
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        
    }
});
英文:

You need to create a custom Spinner adapter and override getView() to set the color and text of each item.

Here I am wrapping colors around the array of colors using the modulus operator (%) in order to avoid IndexOutOfBoundsExecption if the items are greater than the available colors.

The custom adapter

public class ColorAdapter extends BaseAdapter {
private ArrayList&lt;Integer&gt; mColors;
private String[] mItems;
ColorAdapter2(String[] items) {
mItems = items;
String[] hex = {&quot;#886da8&quot;, &quot;#ffc3cf&quot;, &quot;#ef9b00&quot;, &quot;#2bb430&quot;, &quot;#f7dc01&quot;, &quot;#bc65a2&quot;, &quot;#487ebf&quot;, &quot;#e00124&quot;, &quot;#b36634&quot;, &quot;#949494&quot;, &quot;#ffffff&quot;, &quot;#000000&quot;, &quot;#4bbbd0&quot;, &quot;#8b6d9f&quot;, &quot;#8ccff4&quot;};
mColors = new ArrayList&lt;&gt;();
for (String color : hex) {
mColors.add(Color.parseColor(color));
}
}
@Override
public int getCount() {
return mItems.length;
}
@Override
public Object getItem(int args) {
return mColors.get(args);
}
@Override
public long getItemId(int args) {
return args;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
convertView = inflater.inflate(android.R.layout.simple_spinner_dropdown_item, null);
holder = new ViewHolder();
holder.textView = convertView.findViewById(android.R.id.text1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setBackgroundColor(mColors.get(position % mColors.size()));
holder.textView.setText( mItems[position]);
return convertView;
}
/* Return spinner text of the item at particular position*/
public String getItemAtPosition(int position) {
if (position &gt; -1 &amp;&amp; position &lt; mItems.length)
return mItems[position];
else
return null;
}
static class ViewHolder {
TextView textView;
}
}

Usage

Spinner spinner = findViewById(R.id.spinner);
spinner.setAdapter(new ColorAdapter(getResources().getStringArray(R.array.myItems)));

strings.xml

&lt;resources&gt;
&lt;string name=&quot;app_name&quot;&gt;Color Activity&lt;/string&gt;
&lt;string-array name=&quot;myItems&quot;&gt;
&lt;item&gt;item1&lt;/item&gt;
&lt;item&gt;item2&lt;/item&gt;
&lt;item&gt;item3&lt;/item&gt;
&lt;item&gt;item4&lt;/item&gt;
&lt;item&gt;item5&lt;/item&gt;
&lt;/string-array&gt;
&lt;/resources&gt;

Result

个别微调旋转器项目的背景颜色

> Edit
>
> How can I get the text of the selected item?
> spinner.getItemAtPosition(spinner.getSelectedItemPosition()).toString()
> isn´t working anymore. The returned value is a big number.

Now as we created a custom adapter, then you have to customize any related data and you can't rely on the API of the default Spinner adapter; so I've added a getItemAtPosition() method above at the custom adapter that takes a position, and returns the text of this item.

Usage

String item = ((ColorAdapter)spinner.getAdapter()).getItemAtPosition(position));

And you can get it whenever an item is selected:

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) {
Log.d(TAG, &quot;onItemSelected: &quot; + 
((ColorAdapter)spinner.getAdapter()).getItemAtPosition(position));
@Override
public void onNothingSelected(AdapterView&lt;?&gt; parent) {
}
});

huangapple
  • 本文由 发表于 2020年4月7日 05:54:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/61069560.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定