如何动态地将主题添加到Chip View Android

时间:2019-09-19 07:46:16

标签: android material-design material-components-android material-components android-chips

我在 xml 布局中拥有芯片组,并且需要动态添加芯片。我需要动态添加主题,而无需更改应用程序主题我想为芯片视图设置材质主题直到现在我找不到合适的解决方案,我将显示我的代码段。我急需您的帮助

Chip chip = new Chip(mContext);
        chip.setCloseIconVisible(true);
        chip.setText(dataOptions.getListTitle());
        chip.setTextAppearance(mContext, R.style.button_style_value_color_black);
        chip.setHeight(30);
        chip.isCloseIconVisible();
        chip.setChipStrokeColorResource(R.color.chip_stroke_color);
        chip.setChipStrokeWidth(1f);
        chip.setCloseIconTintResource(R.color.grey_500);
        chip.setChipBackgroundColorResource(R.color.chip_background);
        chip.setChipCornerRadius(10f);
        mBinding.feNotesChip.addView(chip);

   chip.setOnCloseIconClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mAdapter.setItemToTheList(dataOptions, mBinding.feNotesEt.getText());
                mBinding.feNotesChip.removeView(chip);
                dataOptionsList.remove(dataOptions);
                saveToFormElement(dataOptionsList, newList);
            }
        });

1 个答案:

答案 0 :(得分:0)

使用该库(https://github.com/Plumillon/ChipView) 您可以从其适配器(即ChipViewAdapter)扩展并覆盖所需的方法,我曾经在特定条件下更改背景

@Override     public int getBackgroundRes(int position){         如果(chipList.get(position).getChipType()。equalsIgnoreCase(AppConstants.LOCATION)){             返回drawable1;         }         返回drawable2;     }