ram*_*dia 12 android kotlin android-chips
我有一个类Question,其中包含一个String数组标签.我将尝试使用Kotlin和新芯片中的每个标签在recyclerview中显示每个问题.这款芯片将包含在chipGroup内.
我的问题是.
如何将阵列的每个标签元素添加到新芯片中?我正在尝试这样做,但显然不起作用.
if (tags != null) {
for (tag in tags) {
val chip = Chip(itemView.context)
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢大家!
kan*_*idj 32
您可以Chip像其他任何方式一样添加s ViewGroup:
for (index in tags.indices) {
val chip = Chip(chipGroup.context)
chip.text= "Item ${tags[index]}"
// necessary to get single selection working
chip.isClickable = true
chip.isCheckable = true
chipGroup.addView(chip)
}
Run Code Online (Sandbox Code Playgroud)
对于singleSelection,请不要忘记添加到您的chipGroup:
chipGroup.isSingleSelection = true
Run Code Online (Sandbox Code Playgroud)
或者在xml中
app:singleSelection="true"
Run Code Online (Sandbox Code Playgroud)
祝你好运,快乐的编码!
尝试创建新芯片时,我总是遇到以下错误:
IllegalArgumentException:此组件要求您指定有效的 android:textAppearance 属性
可以通过R.layout.chip使用以下行来扩展自定义来解决此问题:
android:textAppearance="@style/TextAppearance.MaterialComponents.Chip"
| 归档时间: |
|
| 查看次数: |
7215 次 |
| 最近记录: |