无法更改com.google.android.material.chip.Chip更改印刷机颜色

时间:2019-05-08 11:36:23

标签: android

我以编程方式使用芯片(材料组件),使用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>

1 个答案:

答案 0 :(得分:0)

您必须使用 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>