不在 Material 组件芯片 Android 上显示勾选图标

Do not show Checked Icon On Material Components Chip Android

编辑:我设法通过在 styles.xml 中设置以下属性使其工作,如下所示:

  <android.support.design.chip.Chip
                    android:id="@+id/chipFollowing"
                    style="@style/ChipCustomStyle" ...>

styles.xml

<style name="ChipCustomStyle" parent="Widget.MaterialComponents.Chip.Action">
    <item name="checkedIconEnabled">false</item>
    <item name="checkedIcon">@null</item>
</style>

留在这里以防有人遇到同样的 WTF :)


原问题:

我不想在我的芯片上显示选中的图标。我试过设置

        app:checkedIcon="@null"
        app:checkedIconVisible="false"

在 Chip 和 ChipGroup 元素中。它甚至不会编译:/

当我在 Chip 元素上设置它时,我得到: 错误:找不到属性 'com.companyname.projectname:checkedIconVisible'。

这是我的筹码XML:

            <android.support.design.chip.Chip
                android:id="@+id/chipFollowing"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checkable="true"
                android:clickable="true"
                android:focusable="true"
                android:textAppearance="@style/ChipFilledUncheckedText"
                app:checkedIcon="@null"
                app:checkedIconVisible="false"
                app:chipBackgroundColor="@color/bg_chip_state_list"
                app:chipText="@string/chip_following" />

我正在使用来自 28 支持库版本的 Material 组件。我错过了一些明显的东西吗? :/

我设法通过在 styles.xml 中设置以下属性使其工作,如下所示:

  <android.support.design.chip.Chip
                    android:id="@+id/chipFollowing"
                    style="@style/ChipCustomStyle" ...>

styles.xml

<style name="ChipCustomStyle" parent="Widget.MaterialComponents.Chip.Action">
    <item name="checkedIconEnabled">false</item>
    <item name="checkedIcon">@null</item>
</style>

将您的应用主题从 Theme.AppCompat. 更改为 Theme.MaterialComponents.

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="chipIconTint">@color/colorAccent</item>
</style>

您可以只使用 setCheckable 和 setCheckedIconVisible

示例:

for (int i = 0; i < array.size(); i++) {
KeyValueSelectedEntity letter = array.get(i);
if (getContext() != null) {
    Chip chip = new Chip(getContext());
    chip.setId(letter.getId());
    chip.setText(letter.getName());
    chip.setTag(i);
    chip.setCheckable(true);
    chip.setCheckedIconVisible(false);
    chip.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean selected) {
            Log.d(TAG, "onCheckedChanged");
            int tag = (int) compoundButton.getTag();
            ...
}}