设置com.google.android.material.chip.Chip选择的颜色

问题描述 投票:7回答:3

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

enter image description here

原始文件here

<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>
android material-design
3个回答
14
投票

只需设置一个属性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才能完成这项工作


4
投票

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

除此之外,应用于所有视图的一种常见样式允许您在一个立即对所有视图应用的位置进行更改

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

之后,您只需要为所有Chip视图应用自定义样式。

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

1
投票

对于那些使用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>
© www.soinside.com 2019 - 2024. All rights reserved.