微调项的单独背景颜色
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"};
//color for the first spinner item: hex[0]
//color for the second spinner item: hex[1]
我怎样才能以最好的方式做到这一点?
尝试根据位置改变颜色。
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);
您需要创建自定义 Spinner
适配器并覆盖 getView()
以设置每个项目的颜色和文本。
在这里,我使用模运算符 (%) 将颜色环绕在颜色数组周围,以避免 IndexOutOfBoundsExecption
如果项目大于可用颜色。
自定义适配器
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)));
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>
结果
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.
既然我们创建了自定义适配器,那么您必须自定义任何相关数据,并且不能依赖默认 Spinner 适配器的 API;所以我在上面的自定义适配器上添加了一个 getItemAtPosition()
方法,它占据了一个位置,returns 此项的文本。
用法
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) {
}
});
我想为旋转器个体的每个项目的背景着色。 使用此代码设置项目:
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"};
//color for the first spinner item: hex[0]
//color for the second spinner item: hex[1]
我怎样才能以最好的方式做到这一点?
尝试根据位置改变颜色。
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);
您需要创建自定义 Spinner
适配器并覆盖 getView()
以设置每个项目的颜色和文本。
在这里,我使用模运算符 (%) 将颜色环绕在颜色数组周围,以避免 IndexOutOfBoundsExecption
如果项目大于可用颜色。
自定义适配器
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)));
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>
结果
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.
既然我们创建了自定义适配器,那么您必须自定义任何相关数据,并且不能依赖默认 Spinner 适配器的 API;所以我在上面的自定义适配器上添加了一个 getItemAtPosition()
方法,它占据了一个位置,returns 此项的文本。
用法
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) {
}
});