【问题标题】:This component requires that you specify a valid TextAppearance attribute此组件要求您指定有效的 TextAppearance 属性
【发布时间】:2019-11-01 12:04:55
【问题描述】:

我的应用主题扩展自 AppCompat,但我想动态使用 Chip,但是当我动态添加 Chip 时应用崩溃并出现异常

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

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

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

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

谢谢

【问题讨论】:

  • 芯片(parent.context, null, R.style.Theme_MaterialComponents )
  • 不工作应用程序崩溃

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


【解决方案1】:

您可以查看official documentation

您的应用主题应继承自 Material Components 主题
如果您无法更改您的主题,您可以执行以下操作之一:

对于芯片,您还可以定义如下布局:

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

【讨论】:

  • 我尝试了这两种方法,但它也可以避免崩溃,但在这种情况下芯片过滤器无法按预期工作。即当我单击芯片时,它没有任何效果,没有可绘制的更改,请参阅已编辑的问题
  • 我应该包括可绘制的芯片吗?因为如果我像你提到的那样创建芯片对象,我的应用程序就会崩溃
  • 使用构造函数创建的过滤器芯片在桥主题中无法按预期工作(未命中 onCheckedChangedListener)。您需要使用 xml 文件对其进行膨胀。只有这样你才能捕捉到支票的变化。但是,我仍然不知道为什么它会以这种方式工作。
【解决方案2】:

您可以仅为继承自 Theme.MaterialComponents 的特定芯片指定一个主题,而无需修改您的应用范围的 AppCompat 主题:

<com.google.android.material.chip.Chip
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:theme="@style/Theme.MaterialComponents"/>

【讨论】:

  • 问题是我要创建动态芯片
猜你喜欢
  • 2018-11-22
  • 2015-08-09
  • 1970-01-01
  • 2019-06-23
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
  • 2015-11-27
  • 1970-01-01
相关资源
最近更新 更多