Android-自定义listView,每行颜色不同 [英] Android - custom listView, each row different colours

查看:302
本文介绍了Android-自定义listView,每行颜色不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了自定义列表视图,每一行看起来都像我的文件custom_row.xml.有什么办法,如何分别为每一行设置不同的背景颜色(我需要设置它,因为我的行可以具有不同的值).

I have created custom listview, each row looks like my file custom_row.xml. Is there any way, how to set up different background colors for each row separately (I need to set it because of different values my rows can have).

感谢任何想法

推荐答案

由于您在getView方法中执行了自定义listview使custom_row.xml膨胀后,请更改背景返回inflate方法的视图.请参见下面的示例代码片段:

Since your doing custom listview in the getView method after inflating your custom_row.xml change the background of the return view of inflate method. See sample snippet below:

public getView(int position, View convertView, ViewGroup parent) {
       convertView = getLayoutInflater().inflate(R.layout.custom_xml, null);
       do some stuff...

       //let say you have an arraylist of color
       convertView.setBackgroundColor(arraylist.get(position));

       //in case that your color is limited, just re-use your color again
       //and some logic how to re-use the colors.
}

这篇关于Android-自定义listView,每行颜色不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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