英文:
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<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);
The hex values are stored in an array. Whereas the index count refers to the item count. E.g.:
String[] hex = {"#886da8", "#ffc3cf","#ef9b00", "#2bb430", "#f7dc01", "#bc65a2", "#487ebf","#e00124","#b36634","#949494", "#ffffff","#000000","#4bbbd0", "#8b6d9f", "#8ccff4"};
//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<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);
答案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<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;
}
}
Usage
Spinner spinner = findViewById(R.id.spinner);
spinner.setAdapter(new ColorAdapter(getResources().getStringArray(R.array.myItems)));
strings.xml
<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>
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<?> parent, View view, int position, long id) {
Log.d(TAG, "onItemSelected: " +
((ColorAdapter)spinner.getAdapter()).getItemAtPosition(position));
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论