问题
I have a simple listview that is created with an Array Adapter and ArrayList;
Is there anyway to to access a certain row in the the list view and then change the text color of the text view that resides in that row in the list view?
I know how to change the text color of a textview but i'm having problems accessing the text view that is inside of the list view
回答1:
If you look at the source for simple_list_item_1, you'll see that it is just a TextView. The source is in:
<sdk-dir>/platforms/<your-platform>/data/res/layout/simple_list_item_1
The ArrayAdapter superclass will return that TextView in its getView method. That means you can subclass ArrayAdapter, and inside your subclass' getView method, you can simply chain to the superclass, cast the View it returns to TextView, and do your thing. For example, if you wanted to set the first three items in your list to textSize 24 and the rest to 14, you could do the following:
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv = (TextView) super.getView(position, convertView, parent);
if (position < 3) {
tv.setTextSize(24.0f);
} else {
tv.setTextSize(14.0f);
}
return tv;
}
If you are using a more complicated View than simple_list_item_1, you can figure out the id's of the elements on the View by examining the source and then call findViewById on the View that is returned by the superclass. For example, two_line_list_item.xml has TextViews with ids of android.R.id.text1
and android.R.id.text2
, so you should be able to get a handle on them as follows:
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
TextView tv1 = (TextView)v.findViewById(android.R.id.text1);
TextView tv2 = (TextView)v.findViewById(android.R.id.text2);
//do what you want with the TextViews
}
回答2:
With a custom list item, in your adapters getView
method you could change the text color easily by calling findViewById(R.id.myText)
and then calling setTextColor
. In fact you can do this with the built in list items, you'd just need to know the ID of the TextView... which I don't offhand but you should be able to find it easily enough.
For changing it in XML see Applying Styles and Themes to change the text color if you are using a standard ListView item.
来源:https://stackoverflow.com/questions/5399781/change-text-color-in-listview