【问题标题】:CreateFromRessource ChipDrawable : The style on this component requires your app theme to be Theme.MaterialComponentsCreateFromRessource ChipDrawable :此组件上的样式要求您的应用主题为 Theme.MaterialComponents
【发布时间】:2021-05-01 21:37:32
【问题描述】:

使用 AppCompat 并使用 min sdk 版本 21,我尝试使用 ChipDrawable 在 TextEdit 中的电子邮件地址列表中使用,并使用以下方法创建该 ChipDrawable:

ChipDrawable chip = ChipDrawable.createFromResource(getApplicationContext(), R.xml.email_chip);

使用这个 xml:

<?xml version="1.0" encoding="utf-8"?>
<chip xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:chipBackgroundColor="?colorAccent"
    app:closeIconEnabled="true"
    app:closeIconTint="@android:color/white" />

我的主题使用Theme.MaterialComponents.DayNight.DarkActionBar(我也尝试过Theme.MaterialComponents.DayNight.DarkActionBar.Bridge

但我总是收到这个错误:

The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).

我尝试了在 stackoverflow 上找到的所有内容,但没有太大帮助。

这是我的风格

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.Mareu" parent="Theme.MaterialComponents.DayNight.DarkActionBar.Bridge">
        <!-- Primary brand color. -->
        <item name="colorPrimary">#4E92DF</item>
        <item name="colorPrimaryVariant">#3F7ABD</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
    </style>
</resources>

这是我毕业的一部分

dependencies {
    // Extra
    implementation 'org.greenrobot:eventbus:3.1.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
    implementation 'com.google.android.material:material:1.2.1'

    // Compat
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'

    // TU TI
    testImplementation 'org.hamcrest:java-hamcrest:2.0.0.0'

    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

【问题讨论】:

  • 你能显示你的themes.xml文件吗?
  • @DrHowdyDoo 我在原帖里加了
  • 你用的是什么材料设计版本?
  • 我用的是最后一个稳定的 1.2.1,我也试过用 1.3.rc1。我也将我的 graddle 添加到原始帖子中

标签: java android material-design android-theme android-chips


【解决方案1】:

问题就在这里:

ChipDrawable chip = ChipDrawable.createFromResource(getApplicationContext(),

ApplicationContext没有您的应用主题。
您需要传递Activity,而不是应用程序上下文。

【讨论】:

    【解决方案2】:

    你肯定在你的应用程序中使用了视图绑定,并且你正在用

    来膨胀你的视图
    getLayoutInflater()
    

    尝试从活动而不是应用程序中获取您的充气机,只需使用

    (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)
    

    【讨论】:

      猜你喜欢
      • 2019-04-27
      • 1970-01-01
      • 2021-06-18
      • 2023-03-18
      • 2021-05-09
      • 1970-01-01
      • 2022-07-12
      相关资源
      最近更新 更多