简体   繁体   English

无法更改 com.google.android.material.chip.Chip 更改新闻颜色

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

I programmatically Chips (Material Components), use setChipBackgroundColor change chip state press color but it's have two press color effect, i want to remove default press gray color我以编程方式芯片(材料组件),使用setChipBackgroundColor更改芯片状态按颜色但它有两种按颜色效果,我想删除默认按灰色

  1. my custom press (#daecff)我的自定义印刷机 (#daecff)
  2. default press color (gray)默认印刷颜色(灰色)

thanks谢谢

https://i.imgur.com/5z94oUA.jpg 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>

You have to use the setRippleColor method:您必须使用setRippleColor方法:

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

with something like:像这样:

<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>

Also you have to check the selector used in the setChipBackgroundColor method, in particular the color used for the selected state:您还必须检查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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM