如何以编程方式更改ListView内的TextView的背景颜色? [英] How do you change the background color of a TextView that is inside a ListView Programmatically?

查看:92
本文介绍了如何以编程方式更改ListView内的TextView的背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个TextView项的ListView.该列表是在运行时创建的,大小可能有所不同.我想基于运行时生成的浮点值设置TextView项目的背景.我正在使用ArrayAdapter.

I have a ListView that contains several TextView items. This list is created at runtime, and can vary in size. I would like to set the background of a TextView item based on a float value generated at runtime. I am using an ArrayAdapter.

setListAdapter(new ArrayAdapter<String>(this, R.layout.list_fruit,ratios));  
final ListView listView = getListView();
listView.setTextFilterEnabled(true);
listView.setBackgroundColor(Color.LTGRAY);
((TextView) listView.getChildAt(0)).setBackgroundColor(Color.CYAN);

最后一行抛出NullPointerException.由于某些原因,我无法在listView中访问此TextView.如果我不知道运行时的颜色,应该如何动态设置TextView的背景颜色?

The last line throws a NullPointerException. For some reason I cannot access this TextView inside the listView. How am I supposed to set the background color of a TextView dynamically if I don't know the color until runtime?

推荐答案

只需创建自定义适配器.

Simply you have create custom Adapter.

引用此链接,

如何在ListView上更改颜色和字体

这篇关于如何以编程方式更改ListView内的TextView的背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆