1

我在自定义列表视图中有一个文本视图,我知道如何设置文本颜色,以便更改列表中的所有文本视图。但是现在,我只想让一个特定的视图改变颜色,比如列表视图中有 10 个项目,我只想改变第二个文本视图的颜色,其余的保持不变。任何想法?非常感谢大家的帮助~

public class CheckWinNoAdapter extends ArrayAdapter<String> {
private final Context context;
private String[] values;



public CheckWinNoAdapter(Context context, String[] values) {
    // TODO Auto-generated constructor stub
    super(context, R.layout.list_draw, values);
    this.context = context;
    this.values = values;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.list_draw, parent, false);
    TextView textView1 = (TextView) rowView.findViewById(R.id.chk_tv1);


    textView1.setText(values[position]);




}

}

4

4 回答 4

6

回收其ListView视图以避免浪费内存。因此,与其每次 getView调用时都膨胀一个新视图,不如将其分配给前一个参数convertView,然后重用它。例如,这convertView是您取消滚动的视图。因此,如果 TextView 推迟滚动与具有不同颜色的文本视图相同,则可能会在您执行 defaultcolor 的文本视图中看到“yourcolor”。所以这里每次getView都需要设置的称为文本颜色。

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


    if (converView == null) {
          LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          converView  = inflater.inflate(R.layout.list_draw, parent, false);
    }

    TextView textView1 = (TextView) converView.findViewById(R.id.chk_tv1);

    int color = (position == YOUR_POSITION) ? yourcolor : defaultcolor;
    textView1.setTextColor(color);

    textView1.setText(values[position]);

}
于 2012-07-17T07:54:29.333 回答
4

您可以将您的 getView 更改为这个。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.list_draw, parent, false);
    TextView textView1 = (TextView) rowView.findViewById(R.id.chk_tv1);

    if(position== 2){
        textView1.setColor(Color.White);//or whatever you like
    }
    textView1.setText(values[position]);




}

}

于 2012-07-17T07:57:08.313 回答
3

在获取视图中这样做:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View rowView = inflater.inflate(R.layout.list_draw, parent, false);
TextView textView1 = (TextView) rowView.findViewById(R.id.chk_tv1);


textView1.setText(values[position]);

if(Your Condition Goes Here){
textView.setTextColor(Color.RED); // Did you tried this???
}

}
于 2012-07-17T07:54:16.683 回答
1

而不是传递位置传递列表视图的整数值,您尝试将文本颜色设置为:

if (position == INT_VALUE)//where INT_VALUE=the position at which u want to setcolor of yours
   textView.setTextColor(color);//which color u want to set 
else
  textView.setTextColor(defaultcolor);//to remaining texts
于 2012-07-17T07:59:50.700 回答