安卓材料芯片组件导致应用崩溃。无法膨胀XML。

58

尝试使用来自支持库 com.google.android.support:design:28.0.0-rc01 和材料组件 com.google.android.material:material:1.0.0-rc01 中的 Chip 组件。

StackTrace

android.view.InflateException: Binary XML file line #72: Binary XML file 
      line #72: Error inflating class com.google.android.material.chip.Chip 
      at android.view.LayoutInflater.inflate(LayoutInflater.java:551)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:429)`
版面设计
<com.google.android.material.chip.Chip
    android:id="@+id/chip"
    style="style/Widget.MaterialComponents.Chip.Entry"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/account"
    app:closeIconEnabled="true" />`

你尝试过使用 implementation 'com.android.support:design:28.0.0-rc01' 吗? - Mohamed Mohaideen AH
是的,我做了。同样的问题。 - Sourin Ghosh
检查我的答案 - Mohamed Mohaideen AH
你是否正在扩展提供的主题,如 https://material.io/develop/android/docs/getting-started/ 中所解释的那样? - Paranoid42
@Paranoid42 谢谢你的信息。实际上我还没有这样做。检查后会回复你。 - Sourin Ghosh
@Paranoid42 谢谢,老铁。完美地解决了问题。 - Sourin Ghosh
5个回答

117

请更新您的应用程序主题,使其继承以下其中一种主题:

Theme.MaterialComponents
Theme.MaterialComponents.NoActionBar
Theme.MaterialComponents.Light
Theme.MaterialComponents.Light.NoActionBar
Theme.MaterialComponents.Light.DarkActionBar
例如:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
注意:使用Material Components主题可以启用自定义视图填充器。
来源:https://www.material.io/develop/android/docs/getting-started/

1
我在想为什么应用程序中没有任何com.google.android.material小部件被填充。这就是原因! - OhhhThatVarun
在我的情况下,尽管我使用了 Material 主题,但 val chip = new Chip(context)val chip = layoutInflater.from(context).inflate 都失败了。这是因为我传递了错误的 context(应用程序上下文而不是活动上下文)。因此,我的 Material 主题还没有被应用。 - rmirabelle
*.Bridge 在大多数情况下更好。 "如果您使用桥接主题,则可以在不更改应用程序主题的情况下开始使用 Material Design 组件" https://material.io/develop/android/docs/getting-started#bridge-themes - Zhou Hongbo
@ZhouHongbo,我把Theme.MaterialComponents.Light.NoActionBar改成了Theme.MaterialComponents.Light.NoActionBar.Bridge,结果应用程序崩溃了。:-) - undefined

82

您可以在xml布局中添加@style/Theme.MaterialComponents.Light样式属性,例如:

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

3
谢谢!它可以完美运行,而不需要更改应用程序主题属性。 - Suhas Wagre
获取此组件的样式需要您的应用程序主题为Theme.MaterialComponents(或其子类)。 - NehaK

5
将以下新的主题属性添加到您现有的应用程序主题中:
<style name="Theme.MyApp" parent="Theme.AppCompat">

    <!-- Original AppCompat attributes. -->
    <item name="colorPrimary">@color/my_app_primary_color</item>
    <item name="colorPrimaryDark">@color/my_app_primary_dark_color</item>
    <item name="colorAccent">@color/my_app_accent_color</item>

    <!-- New MaterialComponents attributes. -->
    <item name="colorSecondary">?attr/colorPrimary</item>
    <item name="scrimBackground">@color/mtrl_scrim_color</item>
    <item name="textAppearanceHeadline1">@style/TextAppearance.MaterialComponents.Headline1</item>
    <item name="textAppearanceHeadline2">@style/TextAppearance.MaterialComponents.Headline2</item>
    <item name="textAppearanceHeadline3">@style/TextAppearance.MaterialComponents.Headline3</item>
    <item name="textAppearanceHeadline4">@style/TextAppearance.MaterialComponents.Headline4</item>
    <item name="textAppearanceHeadline5">@style/TextAppearance.MaterialComponents.Headline5</item>
    <item name="textAppearanceHeadline6">@style/TextAppearance.MaterialComponents.Headline6</item>
    <item name="textAppearanceSubtitle1">@style/TextAppearance.MaterialComponents.Subtitle1</item>
    <item name="textAppearanceSubtitle2">@style/TextAppearance.MaterialComponents.Subtitle2</item>
    <item name="textAppearanceBody1">@style/TextAppearance.MaterialComponents.Body1</item>
    <item name="textAppearanceBody2">@style/TextAppearance.MaterialComponents.Body2</item>
    <item name="textAppearanceCaption">@style/TextAppearance.MaterialComponents.Caption</item>
    <item name="textAppearanceButton">@style/TextAppearance.MaterialComponents.Button</item>
    <item name="textAppearanceOverline">@style/TextAppearance.MaterialComponents.Overline</item>

</style>

来源:https://material.io/develop/android/docs/getting-started/
感谢@Paranoid42。

这是关于在Android应用中使用Material Design的入门指南。 Material Design是一种设计语言,旨在为用户提供更加直观、自然和一致的体验。 它可以帮助您创建优秀的用户界面,并且可以适应不同的设备屏幕大小和形状。 现在,让我们开始吧!

最初的问题就在于这个.. 我使用了appcompat主题。 - Sourin Ghosh
添加这些内容时,使用芯片会出现黑屏,但至少不会崩溃。 - RamPrasadBismil

4

最终找到了一种使用新材料芯片动态生成的解决方案

谷歌没有提供任何关于chipsInput布局或如何制作类似gmail地址的布局的文档。使用ChipDrawable不会在芯片选项上提供删除操作,因为无法调用setOnCloseIconClickListener。我使用HorizontalScrollView、ChipGroup和EditText实现了这个功能。

以下是我的代码示例:

MaterialChipsInputDemo


1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接