【问题标题】:How to add theme dynamically to chip view android [duplicate]如何将主题动态添加到芯片视图android [重复]
【发布时间】:2019-09-19 07:46:16
【问题描述】:

我在 xml 布局中有芯片组,需要动态添加芯片。我需要动态添加主题,而不更改应用程序主题我想为芯片视图设置材料主题到现在我找不到合适的解决方案,我将显示我的代码 sn-p。我非常需要你的帮助

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

【问题讨论】:

  • 使用这个constructor并传入style
  • @sonnet 第三个参数是不是的风格。它只是应用主题中的属性。

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


【解决方案1】:

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

    @Override
    public int getBackgroundRes(int position) {
        if (chipList.get(position).getChipType().equalsIgnoreCase(AppConstants.LOCATION)) {
            return drawable1;
        }
        return drawable2;
    }

【讨论】:

  • 但是为什么要使用 3rd 方库?
  • 由客户提出。所以,我用了这个。
  • 好的,但是你读过这个问题吗?问题不在于我可以为芯片使用什么库。
  • 是的,我知道,我没有说你必须使用我提到的库,我只是告诉你这就是我使用库的方式
猜你喜欢
  • 2020-09-02
  • 2017-04-06
  • 1970-01-01
  • 2019-12-24
  • 2019-12-22
  • 2011-09-07
  • 2016-05-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多