我创建了自定义 ListView ,每一行看起来都像我的文件 custom_row.xml。有什么办法,如何分别为每一行设置不同的背景颜色(我需要设置它,因为我的行可以有不同的值)。
谢谢你的想法
最佳答案
由于您在 getView 方法中执行自定义 ListView 膨胀你的 custom_row.xml 后改变背景 inflate 方法的返回 View 。请参阅下面的示例 fragment :
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,每行不同颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10213789/