在Android中以编程方式添加FilterChips [英] Add FilterChips programmatically in Android

查看:307
本文介绍了在Android中以编程方式添加FilterChips的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试选择多个芯片.就我而言,我将动态选择,因此必须动态创建芯片.我成功地能够动态创建它.但是由于多重选择需要一个名为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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆