首页 文章

如何在Android中动态添加新的Android芯片?

提问于
浏览
4

我有一个类Question,其中包含一个String数组标签 . 我将尝试使用Kotlin和新芯片中的每个标签在recyclerview中显示每个问题 . 这款芯片将包含在chipGroup内 .

我的问题是 .

如何将阵列的每个标签元素添加到新芯片中?我正在尝试这样做,但显然不起作用 .

if (tags != null) {
    for (tag in tags) {
        val chip = Chip(itemView.context)
    }
}

谢谢大家!

1 回答

  • 11

    您可以像添加任何其他_1150289一样添加 Chip ,如下所示:

    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)
    }
    

    对于singleSelection,请不要忘记添加到您的chipGroup:

    chipGroup.isSingleSelection = true
    

    或者在xml中

    app:singleSelection="true"
    

    祝你好运,快乐的编码!

相关问题