java - 此组件要求您指定有效的 TextAppearance 属性

标签 java android material-components material-components-android android-chips

我的应用主题从 AppCompat 扩展而来,但我想动态使用 Chip,但是当我动态添加 Chips 时,应用程序因异常而崩溃

This component requires that you specify a valid TextAppearance attribute. Update your app theme to inherit from Theme.MaterialComponents (or a descendant).

但是当我将应用程序主题扩展到 Material 应用程序时不会崩溃并且过滤芯片工作正常但我的问题是我不想从 Material 扩展主题因为它会影响我的应用程序用户界面,我如何在这种情况下使用芯片?

编辑: 使用桥接器,它可以避免崩溃,但是当我点击芯片时,它不会改变我被点击的芯片的外观

Chip chip = new Chip(getContext());
        ChipDrawable chipDrawable = ChipDrawable.createFromAttributes(getContext(), null, 0, R.style.Widget_MaterialComponents_Chip_Filter);
        chip.setChipDrawable(chipDrawable);
        chip.setCheckable(true);
        chip.setText(itemArrayList.get(i).getName());
        binding.rvReport.addView(chip);

谢谢

最佳答案

您可以查看 official documentation :

Your app theme should inherit from a Material Components theme.
If you can't change your theme, you can do one of the following:

  • Inherit from one of our Material Components Bridge themes
  • Continue to inherit from an AppCompat theme and add some new theme attributes to your theme.

对于芯片你也可以定义布局如下:

<com.google.android.material.chip.Chip
    xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/Widget.MaterialComponents.Chip.Filter"
    .../>

然后使用这段代码:

Chip chip =
      (Chip) getLayoutInflater().inflate(R.layout.single_chip_layout, chipGroup, false);
//...
chipGroup.addView(chip);

关于java - 此组件要求您指定有效的 TextAppearance 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58658962/

相关文章:

java - 面向对象编程中的访问修饰符

android - 从数据库获取事件并放置在日历 View 上(android)

Android:如何使以编程方式创建的按钮居中?

android - 以编程方式将子菜单添加到 Menuitem - Android

javascript - 如何激活对 Material 设计 Web 组件文本字段的关注?

java - Android 中已弃用的 People 类的替代方案

java - 如果用户输入不是 ArrayList 中的唯一值,如何循环提示?

android - MaterialComponents 中 AppCompat.Button.Borderless 的替代方案是什么?

Android 无法在 Material Snackbar 上调用 setAnchorView

java - 将背景图片添加到 JFrame