我有在个别项目中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);
如何找到标签的看法? – marlar
有一个方法可以让你做到这一点:她的名字是:'findViewByTag();' – Houcine
你的意思是findViewWithTag()?我试过了,但是这种方法也没用。但是,我找到了解决办法,明天我会发布答案。现在是深夜,我需要睡觉:) – marlar