无法更改 com.google.android.material.chip.Chip 更改按键颜色

Can't change com.google.android.material.chip.Chip change press color

我编程芯片(Material 组件),使用 setChipBackgroundColor 更改芯片状态按键颜色但它有两种按键颜色效果,我想删除默认按键灰色

  1. 我的自定义印刷机 (#daecff)
  2. 默认印刷颜色(灰色)

谢谢

https://i.imgur.com/5z94oUA.jpg

Chip chip = new Chip(context);
chip.setText(name);
chip.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
chip.setTextColor(ContextCompat.getColor(context, R.color.chip_color));
chip.setOnClickListener(onClickListener);
chip.setChipStrokeColorResource(R.color.chip_color);
chip.setChipStrokeWidth(5);
chip.setChipBackgroundColor(
    ContextCompat.getColorStateList(context, R.color.bg_chip)
);
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" android:color="#daecff"/>
    <item android:color="@color/white"/>

</selector>

你必须使用setRippleColor方法:

chip.setRippleColor(ContextCompat.getColorStateList(this,R.color.my_selector));

类似的东西:

<selector xmlns:android="http://schemas.android.com/apk/res/android">    
  <item android:alpha="@dimen/mtrl_low_ripple_pressed_alpha" android:color="?attr/colorPrimary" android:state_pressed="true"/>
  <item android:alpha="@dimen/mtrl_low_ripple_focused_alpha" android:color="?attr/colorOnSurface" android:state_focused="true" android:state_hovered="true"/>
  <item android:alpha="@dimen/mtrl_low_ripple_focused_alpha" android:color="?attr/colorOnSurface" android:state_focused="true"/>
  <item android:alpha="@dimen/mtrl_low_ripple_hovered_alpha" android:color="?attr/colorOnSurface" android:state_hovered="true"/>
  <item android:alpha="@dimen/mtrl_low_ripple_default_alpha" android:color="?attr/colorOnSurface"/>
</selector>

您还必须检查 setChipBackgroundColor 方法中使用的选择器,特别是用于 selected 状态的颜色:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <!-- 24% opacity -->
  <item android:alpha="0.24" android:color="@color/custom" android:state_enabled="true" android:state_selected="true"/>
  ....
</selector>