将自定义 chipDrawable 背景设置为 Chip
Set custom chipDrawable background to Chip
我想为 Chip 设置自定义可绘制背景,就像那样 chip.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.bg_cutom_drawable));
但不是working.It报错
java.lang.UnsupportedOperationException: Do not set the background resource; Chip manages its own background drawable.
需要 chipDrawable。如何为其创建 chipDrawable。我尝试但无法找到解决方案。
请建议我,我将不胜感激。
不要使用 android:background
属性。它会被忽略,因为 Chip 管理自己的背景 Drawable
chip document
如果你想改变背景颜色,你可以尝试:
ChipDrawable chipDrawable = (ChipDrawable)chip.getChipDrawable();
chipDrawable.setChipBackgroundColorResource(R.color.colorPrimary);
如果有人在对话框中使用 material 芯片并因上述相同错误而崩溃,那么您需要从样式中删除对话框集的背景属性。
Using this code you can generate multicolor chip.
将此代码添加到 oncreate() 或函数中的任意位置
// "i" is integer value and it will be unique for every chip. In my case I have put in chip in FOR loop that why "i" is there
Chip chip = (Chip) LayoutInflater.from(getActivity()).inflate(R.layout.item_content_lang_chip, null);
chip.setText(contentLangModels.get(i).getContentDisplay());
chip.setId(i);
chip.setChipBackgroundColor(buildColorStateList(getActivity(),"#1e61d5","#2e2e37"));
chipGrpContentType.addView(chip);
Add below function in your activity
public ColorStateList buildColorStateList(Context context, String pressedColorAttr, String defaultColorAttr){
int pressedColor = Color.parseColor(pressedColorAttr);
int defaultColor = Color.parseColor(defaultColorAttr);
return new ColorStateList(
new int[][]{
new int[]{android.R.attr.state_checked},
new int[]{} // this should be empty to make default color as we want
}, new int[]{
pressedColor,
defaultColor
}
);
}
我想为 Chip 设置自定义可绘制背景,就像那样 chip.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.bg_cutom_drawable));
但不是working.It报错
java.lang.UnsupportedOperationException: Do not set the background resource; Chip manages its own background drawable.
需要 chipDrawable。如何为其创建 chipDrawable。我尝试但无法找到解决方案。 请建议我,我将不胜感激。
不要使用 android:background
属性。它会被忽略,因为 Chip 管理自己的背景 Drawable
chip document
如果你想改变背景颜色,你可以尝试:
ChipDrawable chipDrawable = (ChipDrawable)chip.getChipDrawable();
chipDrawable.setChipBackgroundColorResource(R.color.colorPrimary);
如果有人在对话框中使用 material 芯片并因上述相同错误而崩溃,那么您需要从样式中删除对话框集的背景属性。
Using this code you can generate multicolor chip.
将此代码添加到 oncreate() 或函数中的任意位置
// "i" is integer value and it will be unique for every chip. In my case I have put in chip in FOR loop that why "i" is there
Chip chip = (Chip) LayoutInflater.from(getActivity()).inflate(R.layout.item_content_lang_chip, null);
chip.setText(contentLangModels.get(i).getContentDisplay());
chip.setId(i);
chip.setChipBackgroundColor(buildColorStateList(getActivity(),"#1e61d5","#2e2e37"));
chipGrpContentType.addView(chip);
Add below function in your activity
public ColorStateList buildColorStateList(Context context, String pressedColorAttr, String defaultColorAttr){
int pressedColor = Color.parseColor(pressedColorAttr);
int defaultColor = Color.parseColor(defaultColorAttr);
return new ColorStateList(
new int[][]{
new int[]{android.R.attr.state_checked},
new int[]{} // this should be empty to make default color as we want
}, new int[]{
pressedColor,
defaultColor
}
);
}