如何更改列表视图中列表项的颜色
我在 Activity 类中使用 ListView。我没有为 ListView 使用任何 XML 代码。现在我无法更改列表视图中文本项的颜色。
我更改了 ListView 的背景颜色,但无法更改列表项的颜色。我从互联网上获得了一些链接,但无法弄清楚。谁能帮我用一些代码来做到这一点?
我的 Activity 类如下所示:
ListView listView;
// Create an array of Strings, that will be put to our ListActivity
String[] names = new String[] { "India", "Malaysia" };
TextView tv = new TextView(getApplicationContext());
tv.setText("Select Country");
tv.setTextColor(012);
listView = getListView();
listView.addHeaderView(tv);
listView.setCacheColorHint(Color.rgb(36, 33, 32));
listView.setBackgroundColor(Color.rgb(225, 243, 253));
this.setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,names));
}
上面,tv.setTextColor
设置为列表项的标题。它不起作用,因为它要求我传递一个整数值。我可以为颜色传递什么整数值? 任何人都可以建议一些用于更改列表项颜色的代码吗?
I am using a ListView in an Activity class. I'm not using any XML code for the ListView. Now I am unable to change the color of the Text item in the listView.
I changed the background color of the ListView but am unable to change the color of the list items. I got some links from the internet but am not able to figure it out. Can anyone help me to do that with some code?
My Activity class looks like this:
ListView listView;
// Create an array of Strings, that will be put to our ListActivity
String[] names = new String[] { "India", "Malaysia" };
TextView tv = new TextView(getApplicationContext());
tv.setText("Select Country");
tv.setTextColor(012);
listView = getListView();
listView.addHeaderView(tv);
listView.setCacheColorHint(Color.rgb(36, 33, 32));
listView.setBackgroundColor(Color.rgb(225, 243, 253));
this.setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,names));
}
Above, tv.setTextColor
is set for the heading of the list items. And it is not working as it is asking me to pass an integer value. What integer value can I pass for the Color?
Can any one suggest some code for changing the color of the list items?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于彩色列表项,您需要对其进行自定义。为此,准备一个 xml 文件:
custom_listitem.xml
现在您必须在适配器中使用它,例如 -
是的,您可以使用
Color.RED
、Color.YELLOW
等作为默认颜色,我们可以使用 < code>"#3C3C3C"(在 xml 中使用时)或Color.parseColor("3C3C3C")
(以编程方式使用时)用于除默认颜色之外的任何颜色。For having colored listitem,you need to custom it.For that,prepare an xml file:
custom_listitem.xml
Now you have to use this in your adapter like-
And yes,you can use
Color.RED
,Color.YELLOW
etc.for the default colors,our you can use"#3C3C3C"
(while using in xml) orColor.parseColor("3C3C3C")
(while using programatically) for any colors other than default colors.您需要创建一个 CustomListAdapter。
列表项如下所示 (custom_list.xml):
使用 TextView api 根据您的喜好装饰文本
,您将像这样使用它
You need to create a CustomListAdapter.
The list item looks like this (custom_list.xml):
Use the TextView api's to decorate your text to your liking
and you will be using it like this