13

我正在使用 Material Components 来创建 Choice 芯片。我已关注https://material.io/develop/android/components/chip/文档。有足够的东西可以在 XML 中创建芯片,但不知道如何以编程方式创建选择芯片。

我使用以下代码动态创建芯片,但它默认创建动作芯片。

val chip = Chip(activity)
chip.text = ("Chip 1")
chipGpRow.addView(chip)
4

4 回答 4

20

检查迈克评论

Chip否则,您可以使用和 样式定义一个简单的布局 (layout_chip_choice.xml) :

<com.google.android.material.chip.Chip
    xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/Widget.MaterialComponents.Chip.Choice"
    .../>

然后在您的代码中使用:

val chip = inflater.inflate(R.layout.layout_chip_choice, chipGpRow, false) as Chip  
chip.text = ("Chip 1")
chipGpRow.addView(chip)
于 2019-10-05T10:17:48.560 回答
14

在此处输入图像描述

以下是我的代码,希望对你有用:

为芯片创建项目 xml 并在此处添加您想要的 样式 style="@style/Widget.MaterialComponents.Chip.Choice"

item_chip_category.xml

<com.google.android.material.chip.Chip xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/popin"
android:gravity="center"
android:paddingLeft="8dp"
android:paddingRight="8dp"
style="@style/Widget.MaterialComponents.Chip.Choice"
android:textAppearance="?android:attr/textAppearance"
android:textColor="@color/secondaryTextColor"
app:chipBackgroundColor="@color/colorAccent" />

活动.xml

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:fontFamily="@font/popin"
            android:padding="8dp"
            android:text="Python Progrgrams"
            android:textAlignment="center"
            android:textAppearance="@style/TextAppearance.AppCompat.Medium"
            android:textColor="@color/secondaryTextColor"
            android:textStyle="bold" />

            <com.google.android.material.chip.ChipGroup
                android:id="@+id/chipsPrograms"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/text_margin"
                android:paddingStart="@dimen/text_margin"
                android:paddingEnd="@dimen/text_margin"
                app:chipSpacing="8dp"
                app:singleSelection="false">

            </com.google.android.material.chip.ChipGroup>

    </LinearLayout>

活动.java

 public void setCategoryChips(ArrayList<String> categorys) {
    for (String category :
            categorys) {
        Chip mChip = (Chip) this.getLayoutInflater().inflate(R.layout.item_chip_category, null, false);
        mChip.setText(category);
        int paddingDp = (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 10,
                getResources().getDisplayMetrics()
        );
        mChip.setPadding(paddingDp, 0, paddingDp, 0);
        mChip.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

            }
        });
        chipsPrograms.addView(mChip);
    }
}
于 2019-08-02T06:34:02.213 回答
3

您可以 1) 为具有选择样式的芯片创建 xml 布局并在代码中对其进行扩展,类似于目录中的 ChipGroupDemoFragment 示例:github.com/material-components/material-components-android/blob/... 2 ) 创建一个自定义主题,将默认chipStyle 设置为@style/Widget.MaterialComponents.Chip.Choice 我推荐#1,因为它允许您灵活地动态创建多种样式的芯片。

于 2019-03-28T21:07:52.910 回答
0

不喜欢手动设置 checkable 属性而不是风格方式的评论,你会失去其他属性,如波纹颜色和状态列表动画师。用定义的芯片填充布局并包含样式 attr 是目前唯一的方法。

一直在尝试将样式应用于使用context theme wrapper以编程方式创建的 Chip 。这似乎是要走的路,但作为 AppCompatCheckBox 的扩展的 Chip 没有定义 4-arg 构造函数,在我的例子中,默认样式(操作)可以被覆盖。所以芯片总是动作类型><。

于 2021-02-05T17:41:12.457 回答