首页 > 解决方案 > 由于 Material TextView,Android 资源链接失败

问题描述

我想在我的 android 应用程序中使用材料设计。所以我在我的项目中添加了这个依赖:implementation 'com.android.support:design:27.1.1'. 我有相同的compileSdkVersiontargetSdkVersionas27并且也使用相同版本的AppCompatLibrary: implementation 'com.android.support:appcompat-v7:27.1.1'。我也有谷歌的 Maven 存储库:

allprojects {
    repositories {
        google()
        jcenter()

    }
}

此外,我的活动扩展AppCompatActivity并且我有父主题,AppCompat<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">. styles.xml尽管我满足所有这些条件,但当我将材料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>

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

标签: androidmaterial-designandroid-support-libraryandroidxmaterial-components-android

解决方案


您有 2 个选项:

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

对于第一个选项,您必须为Android导入Material Components而不是 Design Support Library。

Android 的 Material Components 是 Android 设计支持库的直接替代品。

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

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

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

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

对于 android 的材料组件,您必须添加新库:

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

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

请记住

注意:随着 Android 9.0(API 级别 28)的发布,有一个名为 AndroidX 的支持库的新版本,它是 Jetpack 的一部分。AndroidX 库包含现有的支持库,还包含最新的 Jetpack 组件。

我们建议在所有新项目中使用 AndroidX 库


推荐阅读