如何动态更改我的ListView项的文字颜色?

5

我有一个列表视图,其中每个项目都在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>

请注意,这三个文本视图具有不同的文本颜色。
现在,根据偏好设置中的一个设置,用户应该能够更改文本视图项的文本颜色。
基本上,我看到有两种方法可以做到这一点。其中一种是使用主题:
<resources>
  <style name="ThemeBlack" parent="@android:style/Theme">
    <item name="android:textColor">#FFFFFF</item>
    <item name="android:typeface">sans</item>
    <item name="android:background">#999999</item>
    <item name="android:textSize">16sp</item>
  </style>
  <style name="ThemeRed" parent="@android:style/Theme">
    <item name="android:textColor">#0000FF</item>
    <item name="android:typeface">sans</item>
    <item name="android:background">#c81111</item>
    <item name="android:textSize">16sp</item>
  </style>
</resources>

例如,在onCreate()方法中:

this.setTheme(R.style.ThemeRed);

这里的问题在于它将所有textview的文本颜色更改为定义在样式中的颜色。换句话说,它们不再有所不同。因此,我的第一个具体问题是:
是否可以以某种方式定义或应用样式,使其适用于具有三个不同颜色文本视图的listview?
另一种方法是通过编程设置文本颜色,而不是使用样式和主题。这是我的第一种方法,我认为它很容易,但我已经苦苦挣扎了几个小时却无济于事。
我已经尝试在ListActivity的onCreate中执行以下操作:
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);

它不会崩溃,但也不起作用!

因此,我的第二个问题是:

如何在代码中更改列表视图项的文本颜色?

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

更新: 我不知道是否有任何区别,但这是如何弹出列表视图的:

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);
3个回答

10

在大量的谷歌搜索和试错之后,我找到了一个相当简单的解决方案。这个例子通过创建一个匿名方法来覆盖适配器的getView方法,但当然也可以声明一个基于SimpleCursorAdapter的新类,并在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;
        }
});

使用匿名方法的好处是它可以在任何适配器上使用,而无需您对特定类进行子类化。

我的代码受到这篇博客文章的启发:http://sudarmuthu.com/blog/using-arrayadapter-and-listview-in-android-applications


1

这是你在询问的内容吗?

    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); // 现在用户可以选择颜色


谢谢,但那不是我想要的。我想改变列表视图项的颜色,例如在我的特定情况下R.id.showsummary。 - marlar
1
有一篇网络文章详细介绍了如何为每个ListView条目显示不同的颜色。也许这正是你想要的。该网址为http://dustinbreese.blogspot.com/2009/12/creating-listview-with-alternating.html。 - Howard Hodson
当你写这篇文章的时候,我正在发布我的答案。核心思想是相同的,即重写getView()方法,然后调用super.getView()并在返回视图之前修改它。我为你点赞! - marlar

0
尝试给你的textView设置一个标签,然后通过标签找到它,最后使用方法setTextColor(Color.RED);来设置颜色。

有一个方法可以让你做到这一点:她的名字是:findViewByTag(); - Houcine
你是指findViewWithTag()吗?我尝试过了,但这种方法也不起作用。不过,我已经找到了一个解决方案,明天我会发布答案。现在已经很晚了,我需要睡觉 :) - marlar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接