我想在文本化身中添加随机颜色,而不是单一的蓝色 enter image description here
我的列表代码就是这个
list = new ArrayList<>();
for(int i =0;i<data.length();i++){
ModelFriendList temp = new ModelFriendList();
temp.setName(data.getJSONObject(i).getString("name"));
temp.setUsername(data.getJSONObject(i).getString("phone"));
temp.setID(data.getJSONObject(i).getInt("UID"));
list.add(temp);
}
答案 0 :(得分:2)
在模型中添加RandomColor的getter setter方法,并在模型中添加以下代码和项目集背景颜色。
for(int i =0;i<data.length();i++){
Random rnd = new Random();
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
ModelFriendList temp = new ModelFriendList();
temp.setName(data.getJSONObject(i).getString("name"));
temp.setUsername(data.getJSONObject(i).getString("phone"));
temp.setID(data.getJSONObject(i).getInt("UID"));
temp.setRandomColor(color);
list.add(temp);
}
答案 1 :(得分:0)
您可以使用随机类
获取随机颜色 Random random = new Random();
int color = Color.argb(255, random .nextInt(256), random .nextInt(256), random .nextInt(256));
之后,您可以在模型中设置这些颜色,并在想要在视图上设置它时获取它。