【问题标题】:Add FilterChips programmatically in Android在 Android 中以编程方式添加 FilterChips
【发布时间】:2018-12-10 10:43:00
【问题描述】:

我正在尝试进行有多种选择的筹码选择。就我而言,我将有动态选择,因此我将不得不动态创建芯片。我成功地动态创建了它。但是由于多选需要一个名为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

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

【问题讨论】:

标签: android kotlin android-chips


【解决方案1】:

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

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

chip_layout.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" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-08
    • 1970-01-01
    • 2012-05-26
    • 1970-01-01
    • 2017-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多