在ListView控件更改文字颜色 [英] Change Text Color in ListView

查看:94
本文介绍了在ListView控件更改文字颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与数组适配器和ArrayList创建了一个简单的列表视图;

I have a simple listview that is created with an Array Adapter and ArrayList;

反正是有来访问列表视图中某一行,然后更改驻留在该行中的列表视图中的文本视图的文本颜色?

Is there anyway to to access a certain row in the the list view and then change the text color of the text view that resides in that row in the list view?

我知道如何改变一个TextView的文本颜色,但我访问文本视图就是列表视图里面有问题。

I know how to change the text color of a textview but i'm having problems accessing the text view that is inside of the list view

推荐答案

如果你看一下源simple_list_item_1管理,你会看到,它只是一个TextView。来源是:

If you look at the source for simple_list_item_1, you'll see that it is just a TextView. The source is in:

<sdk-dir>/platforms/<your-platform>/data/res/layout/simple_list_item_1

一个ArrayAdapter超类将返回的TextView在getView方法。这意味着你可以继承ArrayAdapter,和你的子类中,getView方法,你可以简单地链超,投它返回到TextView的视图,并做你​​的事。例如,如果你想设置的前三个项目在列表中TEXTSIZE 24,其余为14,你可以做到以下几点:

The ArrayAdapter superclass will return that TextView in its getView method. That means you can subclass ArrayAdapter, and inside your subclass' getView method, you can simply chain to the superclass, cast the View it returns to TextView, and do your thing. For example, if you wanted to set the first three items in your list to textSize 24 and the rest to 14, you could do the following:

public View getView(int position, View convertView, ViewGroup parent) {
  TextView tv = (TextView) super.getView(position, convertView, parent);

  if (position < 3) {
    tv.setTextSize(24.0f);
  } else {
    tv.setTextSize(14.0f);
  }
  return tv;
}

如果您使用的是simple_list_item_1管理更复杂的视图,你可以计算出该ID的视图上的元素通过检查源代码,然后调用findViewById上的超类返回的视图。例如,two_line_list_item.xml有TextViews与 android.R.id.text1 android.R.id.text2 ,所以你应该能够得到对他们的处理如下:

If you are using a more complicated View than simple_list_item_1, you can figure out the id's of the elements on the View by examining the source and then call findViewById on the View that is returned by the superclass. For example, two_line_list_item.xml has TextViews with ids of android.R.id.text1 and android.R.id.text2, so you should be able to get a handle on them as follows:

public View getView(int position, View convertView, ViewGroup parent) {
  View v = super.getView(position, convertView, parent);
  TextView tv1 = (TextView)v.findViewById(android.R.id.text1);
  TextView tv2 = (TextView)v.findViewById(android.R.id.text2);

  //do what you want with the TextViews
}

这篇关于在ListView控件更改文字颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆