Android 如何动态更改listview项目的textColor?

Android 如何动态更改listview项目的textColor?,android,listview,Android,Listview,我有一个列表视图,其中各个项目在custom_row_views.xml中定义: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_hei

我有一个列表视图,其中各个项目在custom_row_views.xml中定义:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <TextView android:id="@+id/showtitle"
  android:textSize="17sp"
  android:textStyle="bold"
  android:textColor="#FFFF00"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>
 <TextView android:id="@+id/showdate"
  android:textSize="14sp"
  android:textStyle="italic"
  android:textColor="#CCCCCC"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>
 <TextView android:id="@+id/showsummary"
  android:textSize="17sp"
  android:textStyle="normal"
  android:textColor="#FFFFFF"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>
</LinearLayout>
这里的问题是,它将所有TextView的文本颜色更改为样式中定义的任何颜色。换句话说,它们不再不同了。因此,我的第一个具体问题是:

是否有可能以某种方式定义或应用样式,使其适合具有三种颜色的文本视图的列表视图?

另一种方法是简单地以编程方式设置文本颜色,而不是使用样式和主题。这是我的第一个方法,我认为这会很容易,但我挣扎了好几个小时都没有成功

我在onCreate的ListActivity中尝试了以下操作:

TextView tv = (TextView) findViewById(R.id.showsummary);
tv.setTextColor(Color.RED);
但这使得应用程序崩溃

然后我试了一下:

TextView tv = null;
LayoutInflater inflater = this.getLayoutInflater();
View aView = inflater.inflate(R.layout.custom_row_view, null);
tv = (TextView) aView.findViewById(R.id.showsummary);
tv.setTextColor(Color.RED);
它没有崩溃,但也不工作

所以我的第二个问题是:

如何更改代码中listview项目的文本颜色?

请注意,所有listview项都应具有新颜色;重要的是,项目内的三个单独文本视图应分别着色。换句话说,我并不是试图在listview中设置单个项目的颜色

更新: 我不知道这是否有什么区别,但这就是listview的流行方式:

Cursor showsCursor = mDbHelper.fetchSummaries(mCategory);
String[] from = new String[]{C2CDbAdapter.SUMMARY_TITLE, C2CDbAdapter.SUMMARY_DATE, C2CDbAdapter.SUMMARY_SUMMARY};
int[] to = new int[]{R.id.showtitle, R.id.showdate, R.id.showsummary};
SimpleCursorAdapter shows = new SimpleCursorAdapter(this, R.layout.custom_row_view, showsCursor, from, to);
setListAdapter(shows);

尝试为textView设置标记,然后按标记查找,最后使用方法
setTextColor(color.RED)设置颜色

这就是你要问的吗

    setContentView(R.layout.main);
    TextView messageText = (TextView) findViewById(R.id.mainTextItem1);
    messageText.setText("This is a test");
    messageText.setTextColor(Color.RED);
XML世界始于:

您可以使用switch语句选择颜色。颜色为int型

int myColor=0xffff0000;//这是红色的

int myColor=Color.RED;//这也是


messageText.setTextColor(myColor);//现在用户可以选择一种颜色了,经过大量的谷歌搜索和反复试验,我找到了一个非常简单的解决方案。本例通过创建匿名方法覆盖适配器的getView方法,但当然也可以基于SimpleCrsorAdapter声明一个新类,并在setListAdapter中使用该类

setListAdapter(new SimpleCursorAdapter(this, R.layout.custom_row_view, showsCursor, from, to) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                View row = super.getView(position, convertView, parent);
                // Here we get the textview and set the color
                TextView tv = (TextView) row.findViewById(R.id.showsummary);
                tv.setTextColor(Color.YELLOW);
                return row;
        }
});
匿名方法的好处在于,它可以在任何适配器上使用,而无需对特定类进行子类化


我的代码灵感来自于这篇博文:

谢谢,但这不是我想要的。在中,您希望更改列表视图项的颜色,例如我的特定案例中的R.id.showsummary。网上有一篇文章详细介绍了如何为每个列表视图项显示不同的颜色。也许这就是你想要的。URL是我在发布我自己的答案,而你写这篇文章。中心思想大致相同,即重写getView,然后调用super.getView()并在返回视图之前修改它。我给你+1个!有一种方法可以让你这么做:她的名字是:
findViewByTag()您是指findViewWithTag()?我试过了,但这种方法也不管用。然而,我已经找到了一个解决方案,明天我将发布答案。现在已经很晚了,我需要睡觉:)
    setContentView(R.layout.main);
    TextView messageText = (TextView) findViewById(R.id.mainTextItem1);
    messageText.setText("This is a test");
    messageText.setTextColor(Color.RED);
setListAdapter(new SimpleCursorAdapter(this, R.layout.custom_row_view, showsCursor, from, to) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                View row = super.getView(position, convertView, parent);
                // Here we get the textview and set the color
                TextView tv = (TextView) row.findViewById(R.id.showsummary);
                tv.setTextColor(Color.YELLOW);
                return row;
        }
});