首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在android中动态创建精选芯片

在android中动态创建精选芯片
EN

Stack Overflow用户
提问于 2019-03-26 13:47:24
回答 4查看 12.9K关注 0票数 11

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

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

代码语言:javascript
复制
val chip = Chip(activity)
chip.text = ("Chip 1")
chipGpRow.addView(chip)
EN

回答 4

Stack Overflow用户

发布于 2019-10-05 18:17:49

检查Mike comment

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

代码语言:javascript
复制
<com.google.android.material.chip.Chip
    xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/Widget.MaterialComponents.Chip.Choice"
    .../>

然后在你的代码中使用:

代码语言:javascript
复制
val chip = inflater.inflate(R.layout.layout_chip_choice, chipGpRow, false) as Chip  
chip.text = ("Chip 1")
chipGpRow.addView(chip)
票数 17
EN

Stack Overflow用户

发布于 2019-08-02 14:34:02

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

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

item_chip_category.xml

代码语言:javascript
复制
<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" />

activity.xml

代码语言:javascript
复制
<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>

Activity.java

代码语言:javascript
复制
 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);
    }
}
票数 11
EN

Stack Overflow用户

发布于 2019-03-29 05:07:53

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55350567

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档