55

如何设置选定的 com.google.android.material.chip.Chip 颜色?我不希望它成为默认的灰色。这是一个单一的选择芯片组。

在此处输入图像描述

原始文档在这里

<com.google.android.material.chip.ChipGroup
    android:id="@+id/chipgroup"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="16dp"
    app:checkedChip="@+id/chip_program"
    app:chipSpacingHorizontal="32dp"
    app:chipSpacingVertical="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/detailText"
    app:singleSelection="true">

    <com.google.android.material.chip.Chip
        android:id="@+id/chip_program"
        style="@style/Widget.MaterialComponents.Chip.Choice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Program"
        app:chipEndPadding="16dp"
        app:chipStartPadding="16dp" />

    <com.google.android.material.chip.Chip
        android:id="@+id/chip_normal"
        style="@style/Widget.MaterialComponents.Chip.Choice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/program_normal"
        app:chipEndPadding="16dp"
        app:chipStartPadding="16dp" />
</com.google.android.material.chip.ChipGroup>
4

9 回答 9

77

只需设置一个属性app:chipBackgroundColor并将颜色状态列表传递给它:

<android.support.design.chip.Chip
    android:id="@+id/test"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checkable="true"
    android:clickable="true"
    android:focusable="true"
    app:chipBackgroundColor="@color/bg_chip_state_list"
    app:chipText="Test" />

bg_chip_state_list看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/colorSecondaryLight" android:state_checked="true" />
    <item android:color="@color/colorPrimaryDark" />
</selector>

但是,我还必须着手android:clickable进行true这项工作

于 2018-07-06T03:28:37.600 回答
42

使用ColorStateList是一种正确的方法。我唯一要添加的是使用自定义定义的样式更清晰易读,特别是如果您想自定义一堆属性。

除此之外,一种应用于所有视图的通用样式允许您在一个地方进行更改,立即应用于所有视图

样式.xml

<style name="CustomChipChoice" parent="@style/Widget.MaterialComponents.Chip.Choice">
        <item name="chipBackgroundColor">@color/background_color_chip_state_list</item>
        <item name="android:textColor">@color/text_color_chip_state_list</item>
</style>

text_color_chip_state_list.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true"
        android:color="@color/color_checked" />
    <item android:state_checked="false"
        android:color="@color/color_unchecked" />
</selector>

background_color_chip_state_list.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/color1" android:state_checked="true" />
    <item android:color="@color/color2" />
</selector>

之后,您需要为所有像这样的芯片视图应用您的自定义样式。

<android.support.design.chip.Chip
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    style="@style/CustomChipChoice"
    android:checkable="true"
    android:clickable="true"
    android:focusable="true"
    android:text="Chip text" />
于 2018-09-12T08:59:41.987 回答
11

要更改中的颜色,Chip您可以使用自定义样式:

    <com.google.android.material.chip.Chip
        style="@style/My_Widget.MaterialComponents.Chip.Choice"
         ../>

使用这种风格:

 <style name="My_Widget.MaterialComponents.Chip.Choice" parent="Widget.MaterialComponents.Chip.Choice">
    <!-- Chip background color selector -->
    <item name="chipBackgroundColor">@color/my_choice_chip_background_color</item>
    <!-- Border color -->
    <item name="chipStrokeColor">@color/primaryDarkColor</item>

    <!-- Chip text color selector -->
    <item name="android:textColor">@color/mtrl_choice_chip_text_color</item>
    <!-- Chip close icon color selector -->
    <item name="closeIconTint">@color/mtrl_chip_close_icon_tint</item>
  </style>

对于chipBackgroundColor你可以使用这样的选择器:

<?xml version="1.0" encoding="utf-8"?>
<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"/>
  <item android:alpha="0.24" android:color="@color/secondaryDarkColor" android:state_enabled="true" android:state_checked="true"/>
  <!-- 12% of 87% opacity -->
  <item android:alpha="0.10" android:color="@color/primaryLightColor" android:state_enabled="true"/>
  <item android:alpha="0.12" android:color="@color/colorPrimary"/>

</selector>

对于文本颜色,您可以使用以下内容:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

  <item android:color="@color/colorAccent" android:state_enabled="true" android:state_selected="true"/>
  <item android:color="?attr/colorPrimary" android:state_enabled="true" android:state_checked="true"/>
  <!-- 87% opacity. -->
  <item android:alpha="0.87" android:color="?attr/colorOnSurface" android:state_enabled="true"/>
  <!-- 38% of 87% opacity. -->
  <item android:alpha="0.33" android:color="?attr/colorOnSurface"/>

</selector>

正常/选定状态的结果:

在此处输入图像描述在此处输入图像描述

于 2019-09-24T10:02:52.410 回答
5

对于那些使用alpha-05,我发现在可过滤( )芯片state_checked上被忽略了。parent="Widget.MaterialComponents.Chip.Filter"相反,您需要state_selected

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:color="@color/apricot" android:state_selected="true"/>
  <item android:color="@color/apricotSubtle"/>
</selector>
于 2019-04-23T14:25:30.757 回答
1

正如其他人提到的,您需要将芯片元素的背景颜色属性设置为您定义的 ColorStateList。但我只是想指出一个关于如何做到这一点的重要说明,因为我遇到了让不同状态工作的问题。

在定义您自己的 ColorStateList(xml 资源)时,您需要确保在默认颜色之前在 ColorStateList 中设置不同的状态选项!在我弄清楚之前,这让我绊倒了几天,所以我希望这对其他人也有帮助。

此外,您的芯片需要可点击和聚焦(可检查对我不起作用),因此也将这些属性设置为 true。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true"
          android:color="@color/chipColorLight" />
    <item android:color="@color/chipColorDefault"/>
</selector>

如果您想以编程方式设置不同的 ColorStateOptions,您可以这样做:

binding.myChip.chipBackgroundColor = resources.getColorStateList(R.color.chip_color_state_list)
于 2020-08-17T06:46:56.523 回答
0

不知何故改变android:textColorstyles我不起作用。我必须以编程方式更改芯片的文本颜色(因为我也以编程方式创建芯片)。

val chip = Chip(context)
// Apply custom MyChipChoice style to the chip
val drawable = ChipDrawable.createFromAttributes(context!!, null, 0, R.style.MyChipChoice)
chip.setChipDrawable(drawable)
// Apply text color to the chip
val colorStateList = ContextCompat.getColorStateList(context!!, R.color.my_choice_chip_text_color)
chip.setTextColor(colorStateList)
于 2019-11-27T17:08:45.417 回答
0

如果您在代码中创建芯片项,请使用上面提到的状态列表和以下方法(当然是在 Java 中):

chip.setClickable(true);
chip.setCheckable(true);
chip.setChipBackgroundColor(getColorStateList(R.color.chip_background_color));
chip.setCheckedIconVisible(false);

注意: getColorStateList 要求 build.gradle 脚本中的 minSdkVersion 为 23。

于 2021-02-06T18:19:58.580 回答
0

看看这个...

<com.google.android.material.chip.ChipGroup
            android:id="@+id/chipGroupFilter"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:theme="@style/Theme.MaterialComponents.Light.DarkActionBar"
            app:selectionRequired="true"
            app:singleLine="true"
            app:singleSelection="true">

            <com.google.android.material.chip.Chip
                android:id="@+id/chipAll"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="All"
                android:checkable="true"
                android:clickable="true"
                android:focusable="true"
                app:chipBackgroundColor="@color/bg_chip_state_list"
                app:checkedIconEnabled="false"
                android:textColor="@color/whiteBlackSwitchColor"
                app:chipIcon="@drawable/ic_all"
                app:chipIconTint="#4D4F55"
                app:chipIconVisible="true" />
     </com.google.android.material.chip.ChipGroup>
于 2021-05-22T12:10:02.037 回答
0

因此,您可以使用该setChipBackgroundColor(ColorStateList cl)方法设置芯片的颜色,然后您可以添加一个setOnClickListener(new ...)切换选择和非选择,如下面的代码:

yourchip.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (((Chip)v).getChipBackgroundColor().equals(getResources().getColorStateList(R.color.colorPrimaryDark,null))) {
            ((Chip)v).setChipBackgroundColor(getResources().getColorStateList(R.color.colorPrimary, null));
        } else {
            ((Chip) v).setChipBackgroundColor(getResources().getColorStateList(R.color.colorPrimaryDark, null));
        }
    }
});

我用于colorPrimaryDark选择和colorPrimary非选择的地方。

于 2021-07-13T05:33:15.370 回答