SetBackGroundColor 在 CardView 中不起作用
Posted
技术标签:
【中文标题】SetBackGroundColor 在 CardView 中不起作用【英文标题】:SetBackGroundColor doesn't work in CardView 【发布时间】:2022-01-20 21:57:22 【问题描述】:大家好,请帮帮我!!!
我想在 RecyclerView 中更改 CardView 颜色。
Here is the result, but I want to change different color for each CardView
这是我在 onBindViewHolder 中的代码
public void onBindViewHolder(@NonNull CardViewHolder cardViewHolder, int i)
Course course = getListCourses().get(i);
cardViewHolder.tvCourseTitle.setText(course.getCourseTitle());
cardViewHolder.tvCourseShortDescription.setText(course.getCourseShortDescription());
cardViewHolder.imgCourse.setImageResource(course.getCourseImage());
cardViewHolder.cardView.setCardBackgroundColor(Color.parseColor(course.getCourseColor()));
@Override
public int getItemCount()
return getListCourses().size();
public class CardViewHolder extends RecyclerView.ViewHolder
private ImageView imgCourse;
private TextView tvCourseTitle, tvCourseShortDescription;
private CardView cardView, cardViewContainer;
public CardViewHolder (@NonNull View itemView)
super(itemView);
imgCourse = itemView.findViewById(R.id.img_item_course);
tvCourseTitle = itemView.findViewById(R.id.tv_item_title);
tvCourseShortDescription = itemView.findViewById(R.id.tv_item_short_description);
cardView = itemView.findViewById(R.id.cardView_background);
cardViewContainer = itemView.findViewById(R.id.container);
这是Java.lang中的代码
String courseTitle, courseShortDescription, courseColor;
int courseImage;
public Course(String title, String description, int image, String color)
this.courseTitle = title;
this.courseShortDescription = description;
this.courseImage = image;
this.courseColor = color;
public String getCourseTitle()
return courseTitle;
public void setCourseTitle(String courseTitle)
this.courseTitle = courseTitle;
public String getCourseShortDescription()
return courseShortDescription;
public void setCourseShortDescription(String courseShortDescription)
this.courseShortDescription = courseShortDescription;
public String getCourseColor()
return courseColor;
public void setCourseColor(String courseColor)
this.courseColor = courseColor;
public int getCourseImage()
return courseImage;
public void setCourseImage(int courseImage)
this.courseImage = courseImage;
注意当我使用方法 cardViewHolder.cardView.setCardBackgroundColor(Color.parseColor(course.getCourseColor())); , 应用被毁了
【问题讨论】:
【参考方案1】:每个卡片视图的颜色不同,您可以在您的onBindViewHolder()
中使用这样的条件设置
if (course.getCourseTitle().equalsIgnoreCase("Hello android"))
cardViewHolder.cardView.setBackgroundColor(R.color.teal_200);
else if (course.getCourseTitle().equalsIgnoreCase("Hello Python"))
cardViewHolder.cardView.setBackgroundColor(R.color.teal_700);
else if (course.getCourseTitle().equalsIgnoreCase("Android Basic"))
cardViewHolder.cardView.setBackgroundColor(R.color.design_default_color_error);
【讨论】:
以上是关于SetBackGroundColor 在 CardView 中不起作用的主要内容,如果未能解决你的问题,请参考以下文章
在循环中使用 setBackgroundColor 和 setImageDrawable 时 Android 强制关闭 [关闭]
uitableview setbackgroundcolor 未在 UITableViewStyleGrouped 表中设置颜色