2011-07-13 45 views
4

我有在个别项目中custom_row_views.xml定义列表视图:如何动态更改我的列表视图项目的textColor?

<?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> 

注意,三个textviews有不同的文本颜色。

现在,根据偏好设置,用户应该改变文本视图项目的文本颜色。

基本上,我看到两种方式来做到这一点。一种是使用一个主题:

<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); 

这里的问题是,它改变了所有的textviews'文本颜色,无论是在样式定义。换句话说,它们不再是不同的。所以我的第一个具体问题是:

是否有可能以某种方式定义或应用样式,以便迎合包含三个单独颜色文本浏览的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); 

它不会崩溃,但它也不管用!

所以我的第二个问题是:

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

请注意,所有的listview项目应该有新的颜色;重要的是里面的三个单独的文字浏览这些项目应该单独着色。换句话说,我并不是试图在列表视图中设置单个项目的颜色。

UPDATE: 我不知道这有什么差别,但是这是列表视图是如何popuplated:

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); 

回答

5

经过大量的搜索和反复试验,我发现了一个非常简单的解决方案。此示例通过创建匿名方法覆盖适配器的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

0

尝试将标签设置为您的TextView,然后找到它标签,最后使用方法设置颜色setTextColor(Color.RED);

+0

如何找到标签的看法? – marlar

+0

有一个方法可以让你做到这一点:她的名字是:'findViewByTag();' – Houcine

+0

你的意思是findViewWithTag()?我试过了,但是这种方法也没用。但是,我找到了解决办法,明天我会发布答案。现在是深夜,我需要睡觉:) – marlar

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

+0

谢谢,但那不是我想要的。在想要更改列表视图项目的颜色,例如在我的具体情况下R.id.showsummary。 – marlar

+1

网上有一篇文章详细介绍了如何为每个ListView条目显示不同的颜色。也许这就是你想要的。该网址是http://dustinbreese.blogspot.com/2009/12/creating-listview-with-alternating.html –

+0

当您写这篇文章时,我发布了自己的答案。中心思想大致相同,即重写getView,然后调用super.getView()并在返回之前修改视图。我给你+1这个! – marlar

相关问题