Android 资源链接因 Material TextView 而失败

Android resource linking failed due to Material TextView

我想在我的 android 应用程序中使用 material 设计。所以我在我的项目中添加了这个依赖:implementation 'com.android.support:design:27.1.1'。我有与 27 相同的 compileSdkVersiontargetSdkVersion,并且还使用相同版本的 AppCompat 库:implementation 'com.android.support:appcompat-v7:27.1.1'。 我还有 google 的 maven 存储库:

allprojects {
    repositories {
        google()
        jcenter()

    }
}

此外,我的 activity 扩展了 AppCompatActivity 并且我的父主题 AppCompat 作为 styles.xml 中的 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">。尽管我满足所有这些条件,但当我将 material TextView 放入 xml 时:

<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="textfield_label">

    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>

我在设计预览中看到无法识别类型的视图。此外,应用程序在尝试启动时停止。

您有 2 个选择:

  • 使用组件com.google.android.material.textfield.TextInputLayout
  • 使用组件android.support.design.widget.TextInputLayout

对于第一个选项,您必须导入 Material Components for Android 而不是设计支持库。

The Material Components for Android is a drop-in replacement for Android's Design Support Library.

对于第二个选项,只需更改使用的组件:

 <android.support.design.widget.TextInputLayout
   ...>

    <android.support.design.widget.TextInputEditText />

</android.support.design.widget.TextInputLayout>

对于 android 的 material 个组件,您必须添加新库:

  dependencies {
    // ...
    implementation 'com.google.android.material:material:<version>'
    // ...
  }

并检查 the doc 设置(您必须更改 compileSdkVersion、依赖项和主题),还必须迁移到 androidx 库。

Keep in mind that:

Note: With the release of Android 9.0 (API level 28) there is a new version of the support library called AndroidX which is part of Jetpack. The AndroidX library contains the existing support library and also includes the latest Jetpack components.

We recommend using the AndroidX libraries in all new projects

这样使用

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="textfield_label">

     <android.support.design.widget.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

</android.support.design.widget.TextInputLayout>