首先我在循环中创建动态imagview,然后设置背景图像,之后我采用drawable并使用以下代码将颜色设置为我的imageview drawable,
int[] colorcodes = {Color.BLUE, Color.BLACK, Color.GREEN};
for (int i = 0; i < medicine.size(); i++) {
ImageView image = new ImageView(getActivity());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(60, 60);
params.setMargins(10, 20, 10, 20);
image.setLayoutParams(params);
colorOfMedicine = medicineDetails.getMedicineColor();
image.setBackgroundResource(0);
image.setBackgroundResource(R.drawable.ic_tablet);
//Drawable drawable = image.getResources().getDrawable(R.drawable.ic_tablet);
Drawable drawable = image.getBackground();
drawable.clearColorFilter();
//image.invalidate();
//image.setBackgroundColor(colorcodes[i]);
drawable.setColorFilter(new PorterDuffColorFilter(colorcodes[i], PorterDuff.Mode.SRC_IN));
layout.addView(image);
}
但我的问题是,它正在为所有图像设置最后一种颜色。