在android中动态创建Choice Chip

kin*_*tel 10 android android-chips material-components material-components-android

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

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

val chip = Chip(activity)
chip.text = ("Chip 1")
chipGpRow.addView(chip)
Run Code Online (Sandbox Code Playgroud)

Gab*_*tti 16

检查迈克评论

否则,您可以使用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"
    .../>
Run Code Online (Sandbox Code Playgroud)

然后在您的代码中使用:

val chip = inflater.inflate(R.layout.layout_chip_choice, chipGpRow, false) as Chip  
chip.text = ("Chip 1")
chipGpRow.addView(chip)
Run Code Online (Sandbox Code Playgroud)


Nag*_*hal 11

在此处输入图片说明

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

为芯片创建项目 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" />
Run Code Online (Sandbox Code Playgroud)

活动.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>
Run Code Online (Sandbox Code Playgroud)

活动.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);
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 4

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

\n