从数组添加Android芯片

时间:2019-03-16 11:25:38

标签: android android-chips

我有一个像这样的数组:

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

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

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

如果可能的话,请指导我阅读有关此内容的教程。

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

enter image description here

1 个答案:

答案 0 :(得分:0)

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

    for (int i = 0; i < myList.length; i++) {

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