【问题标题】:Dynamically create Choice Chip in android在android中动态创建选择芯片
【发布时间】:2019-08-16 10:45:00
【问题描述】:

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

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

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

【问题讨论】:

  • 看起来没有任何单一的方法可以以编程方式在Chip 上设置特定样式。但是,只需根据Choice 样式中定义的属性设置各个相关属性即可;例如,chip.checkable = true 等。您可以在此处找到这些属性及其值:github.com/material-components/material-components-android/blob/…
  • 迈克 M. 谢谢。它通过设置选择芯片链接中定义的属性来工作。

标签: android material-components-android material-components android-chips


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    为芯片创建项目 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" />
    

    activity.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>
    

    Activity.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);
        }
    }
    

    【讨论】:

    • 有没有办法在适配器而不是活动中做到这一点?
    【解决方案3】:

    检查Mike comment

    否则您可以使用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)
    

    【讨论】:

    • 我真的很喜欢这个答案,因为我不知道您可以以编程方式创建这样的个人视图。我只是想知道,如果我想在远离onCreateView 的某个地方动态创建我的芯片,我应该使用哪个inflater
    • 您可以在ActivityFragment 中使用getLayoutInflater()
    【解决方案4】:

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

    【讨论】:

      猜你喜欢
      • 2021-11-07
      • 2019-12-19
      • 2020-09-02
      • 1970-01-01
      • 1970-01-01
      • 2016-09-06
      • 1970-01-01
      • 2019-09-11
      • 2020-02-29
      相关资源
      最近更新 更多