首页 > 解决方案 > 从数组中添加 android 芯片

问题描述

我有一个这样的数组:

['A','B','C']

我想在我的 android 的标签/芯片中显示它我对这个概念真的很陌生,不确定要使用什么芯片库我只想从数组中获取它并显示如下内容:

for(int i = 0; i<size; i++)
 {
     //dynamically add chips from the array
 }

如果可能的话,将我引向一个教程。

所以我的最终结果应该是这样的:

在此处输入图像描述

标签: androidandroid-chips

解决方案


在这里,我正在为芯片头创建一个字符串数组列表

String []myList = {"Chip1", "Chip2", "Chip3", "Chip4"};

现在我将渲染每个 ArrayList 元素

for (int i = 0; i < myList.length; i++) {
    // Here I am creating Chip view dynamically using current Context
    Chip chip = new Chip(binding.chipGroup.getContext()); 
    LinearLayout.LayoutParams layoutParams= new 
    LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 
    LinearLayout.LayoutParams.WRAP_CONTENT);
    layoutParams.setMargins(5,5,5,5);

    chip.setLayoutParams(layoutParams);
    chip.setText(myList[i]);
    chip.setCloseIconEnabled(true);
    chip.setChipBackgroundColor(getResources().getColorStateList(R.color.colorChipIconTint));
    chip.setTextColor(getResources().getColorStateList(R.color.colorChipText));
    chip.setCloseIconTint(getResources().getColorStateList(R.color.colorChipCloseIcon));

    chip.setClickable(true);
    chip.setCheckable(false);

    binding.chipGroup.addView(chip);
    }



推荐阅读