1

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

1 回答 1

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>
于 2019-09-24T08:05:32.367 回答