2

我正在尝试进行有多种选择的芯片选择。就我而言,我将有动态选择,因此我将不得不动态创建芯片。我成功地动态创建了它。但是由于多选需要一个名为的属性,style="@style/Widget.MaterialComponents.Chip.Filter" 所以我可以在 XML 中传递它,但不能在 kotlin 代码中传递。我试图这样做但没有成功:

val chip = Chip(chapManager.context, null, android.widget.Filter)

它说:Classifier 'Filter' does not have a companion object, and thus must be initialized here

其他一切都很好,只是我无法将风格传递给我的芯片。

4

1 回答 1

1

您可以定义单独的布局Chip并设置您想要的所有属性XML,然后扩展布局。

 val chip = layoutInflater.inflate(R.layout.chip_layout, view!!.parent.parent as ViewGroup, false) as Chip

芯片布局.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.chip.Chip xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/Widget.MaterialComponents.Chip.Filter"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="@style/chipTextAppearance"
    android:textColor="@android:color/black" />
于 2019-03-29T20:09:15.503 回答