在Android中以编程方式添加FilterChips [英] Add FilterChips programmatically in Android
问题描述
我正在尝试选择多个芯片.就我而言,我将动态选择,因此必须动态创建芯片.我成功地能够动态创建它.但是由于多重选择需要一个名为style="@style/Widget.MaterialComponents.Chip.Filter"
的属性
我能够以XML而不是Kotlin代码传递此信息.
我试图这样做,但没有成功:
I am trying to make a Chip Selection which will have multiple choices. In my case I will have choices dynamically so I will have to create chips dynamically. I was successfully able to create it dynamically. But as multi selection needs a property called style="@style/Widget.MaterialComponents.Chip.Filter"
I am able to pass this in XML but not in kotlin code.
I tried to do like this but didn't succeed:
val chip = Chip(chapManager.context, null, android.widget.Filter)
它说:Classifier 'Filter' does not have a companion object, and thus must be initialized here
其他所有方法都可以正常工作,只是我无法将样式传递给芯片.
Everything else works fine, just I'm not able to pass style to my chip.
推荐答案
您可以为Chip
定义单独的布局,并在XML
中设置所需的所有属性,然后对布局进行充气.
You can define separate layout for Chip
and setting all the attributes you want in XML
, then inflate the layout.
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" />
这篇关于在Android中以编程方式添加FilterChips的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!