机器人:微调不同颜色的每个项目 [英] android: each item of spinner different color
问题描述
我有一个微调中,我想有每个项目的文本不同的颜色。例如在item0文字应该是红色的,项目1文本应该是蓝色的,和ITEM2文字应该是绿色的。我试着设置我想换一个TextView的项目和改变的TextView的颜色,但它不工作的方式。如何acomplish这项任务什么想法?
微调微调=(微调)findViewById(R.id.spinner1); ArrayList的<串GT;阵列=新的ArrayList<串GT;();
array.add(item0);
array.add(ITEM1);
array.add(ITEM2); ArrayAdapter<串GT; spinnerArrayAdapter =新ArrayAdapter<串GT;(这一点,R.layout.row,数组);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(spinnerArrayAdapter) 尝试{
TextView的电视=新的TextView((上下文)spinner.getItemAtPosition(0));
tv.setTextColor(Color.argb(0,255,0,0));
}赶上(例外五){
Toast.makeText(getApplicationContext(),错误:+ e.toString(),Toast.LENGTH_LONG);
}
创建自己的类扩展BaseAdapter并实现SpinnerAdapter。
覆盖getDropDownView,正如你处理的位置可以从一个自定义布局,你夸大格式化TextView的。
@覆盖
公共查看getDropDownView(INT位置,查看convertView,父母的ViewGroup){ 视图V = convertView; 如果(V == NULL){
LayoutInflater VI =(LayoutInflater)mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
V = vi.inflate(R.layout.your_custom_layout,NULL);
}
TextView的电视=(TextView中)v.findViewById(R.id.yourTextViewFromYourLayout);
tv.setText(yourArrayList.getItem(位置));
开关(位置){
情况下0:
//这里设置电视的色彩......
打破;
情况1:
//这里设置电视的色彩......
等等...
默认:
//设置默认的颜色或任何...
}
返回伏;
}
I have a spinner in which I would like to have the text of each item be a different color. for example on item0 the text should be red, item1 the text should be blue, and item2 the text should be green. I tried setting the item I want to change to a textView and changing the color of the textview but it does not work that way. any ideas on how to acomplish this task?
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
ArrayList<String> array = new ArrayList<String>();
array.add("item0");
array.add("item1");
array.add("item2");
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,R.layout.row, array);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerArrayAdapter)
try{
TextView tv = new TextView((Context) spinner.getItemAtPosition(0));
tv.setTextColor(Color.argb(0, 255, 0, 0));
}catch(Exception e){
Toast.makeText(getApplicationContext(), "Error: " + e.toString(), Toast.LENGTH_LONG);
}
create your own class that extends BaseAdapter and implements SpinnerAdapter.
Override getDropDownView, and as you process the position you can format the textview from a custom layout you inflate.
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent){
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.your_custom_layout, null);
}
TextView tv=(TextView) v.findViewById(R.id.yourTextViewFromYourLayout);
tv.setText(yourArrayList.getItem(position));
switch (position) {
case 0:
//set tv's color here...
break;
case 1:
//set tv's color here...
etc...
default:
//set default color or whatever...
}
return v;
}
这篇关于机器人:微调不同颜色的每个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!