我有一个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_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项目的文本颜色。
基本上,我认为有两种方法可以做到这一点。一个是使用主题:
<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的文本颜色更改为样式中定义的任何颜色。换句话说,它们不再是不同的。所以我的第一个具体问题是:
是否有可能以某种方式定义或应用样式,以便它们能够满足具有三种具有单独颜色的文本视图的列表视图?
另一种方法是以编程方式设置文本颜色,而不是使用样式和主题。这是我的第一个方法,我认为这很容易,但我已经挣扎了好几个小时但没有用。
我在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项目的文字颜色?
请注意,所有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);
答案 0 :(得分:7)
经过大量的谷歌搜索和反复试验后,我找到了一个非常简单的解决方案。这个例子通过创建一个匿名方法来覆盖适配器的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 :(得分: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); //现在用户可以选择颜色
答案 2 :(得分:0)
尝试将标记设置为textView,然后按标记查找,最后使用方法setTextColor(Color.RED);