在android中动态创建Choice芯片

在android中动态创建Choice芯片,android,material-components-android,material-components,android-chips,Android,Material Components Android,Material Components,Android Chips,我正在使用材料组件来创建Choice芯片。我遵守了这份文件。有足够的东西可以用XML创建芯片,但不知道如何以编程方式创建choice芯片 我使用了下面的代码来动态创建芯片,但默认情况下它会创建动作芯片 val chip = Chip(activity) chip.text = ("Chip 1") chipGpRow.addView(chip) 您可以1)为具有choice样式的芯片创建xml布局,并在代码中对其进行膨胀,与目录中的ChipGroupDemoFragment示例类似:githu

我正在使用材料组件来创建Choice芯片。我遵守了这份文件。有足够的东西可以用XML创建芯片,但不知道如何以编程方式创建choice芯片

我使用了下面的代码来动态创建芯片,但默认情况下它会创建动作芯片

val chip = Chip(activity)
chip.text = ("Chip 1")
chipGpRow.addView(chip)
您可以1)为具有choice样式的芯片创建xml布局,并在代码中对其进行膨胀,与目录中的ChipGroupDemoFragment示例类似:github.com/material components/material components android/blob/…2)创建一个自定义主题,将默认chipStyle设置为@style/Widget.MaterialComponents.Chip.Choice。我建议使用#1,因为它允许您灵活地动态创建多种样式的芯片

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

为芯片创建项目xml并添加所需的样式 比如这里style=“@style/Widget.MaterialComponents.Chip.Choice”

item_chip_category.xml

activity.xml

Activity.java
public void集合类别chips(ArrayList类别){
对于(字符串类别:
(类别){
Chip mChip=(Chip)this.getLayoutFlater().flate(R.layout.item\u Chip\u category,null,false);
mChip.setText(类别);
int paddingDp=(int)TypedValue.applyDimension(
TypedValue.COMPLEX\u UNIT\u倾角,10,
getResources().getDisplayMetrics()
);
mChip.setPadding(paddingDp,0,paddingDp,0);
setOnCheckedChangeListener(新的CompoundButton.OnCheckedChangeListener(){
@凌驾
检查更改后的公共无效(复合按钮复合按钮,布尔b){
}
});
芯片程序。添加视图(mChip);
}
}
请检查

否则,您可以使用
chip
和样式定义一个简单布局(layout\u chip\u choice.xml):

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

如果不喜欢手动设置可检查属性的注释,而不是样式方式,则会丢失其他属性,如ripple color和state list animator。使用芯片定义的布局进行充气,并选择包含样式属性是目前唯一的方法


一直在尝试将样式应用于通过编程方式创建的芯片。这似乎是一条路要走,但作为AppCompatCheckBox扩展的芯片没有定义4-arg构造函数,在我的例子中,默认样式(操作)可以被选项覆盖。因此,芯片总是action>类型,看起来没有任何单一方法可以通过编程方式在
芯片上设置特定样式。然而,这仅仅是根据
选择
样式中定义的属性设置单个相关属性的问题;e、 例如,
chip.checkable=true
,等等。您可以在这里找到这些属性及其值:.Mike M.谢谢。它通过设置选择芯片链接中定义的属性来工作。我真的很喜欢这个答案,因为我不知道你可以通过编程创建这样的个人视图。我只是想知道,如果我想动态创建我的芯片,应该使用哪个
充气机,远离
onCreateView
?你可以在
活动中使用
getLayoutInflater()
片段
。有没有办法在适配器中而不是在活动中这样做?
<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>
 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);
    }
}
<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)