安卓无法解析Material Components

16
我遇到了这个错误:

无法解析符号 '@style/Widget.MaterialComponents.TextInputLayout.OutlineBox'

在我的XML中的TextInputLayout中添加了这行代码后,我遇到了这个错误。
style="@style/Widget.MaterialComponents.TextInputLayout.OutlineBox" 
这是我的完整XML代码(已删除不相关的约束/边距):
<android.support.design.widget.TextInputLayout
    android:id="@+id/textInputLayout2"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlineBox"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:counterEnabled="true"
    app:counterMaxLength="300">

    <android.support.design.widget.TextInputEditText
        android:id="@+id/cheese_description"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="start"
        android:hint="@string/cheese_description" />

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

这是我的应用程序 build.gradle 文件:

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "app.cheese.cheese.some.sample_cheese"
        minSdkVersion 19
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    buildToolsVersion '27.0.3'
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.google.firebase:firebase-auth:11.8.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    compile 'com.android.support:design:27.0.2'
    compile 'com.google.firebase:firebase-core:11.8.0'
    implementation 'com.android.support:appcompat-v7:27.0.2'
    compile 'com.android.support:support-v4:27.0.2'
    compile 'com.google.firebase:firebase-storage:11.8.0'
    compile 'com.google.firebase:firebase-messaging:11.8.0'
    compile 'com.firebaseui:firebase-ui-auth:3.1.3'
    compile 'com.google.firebase:firebase-database:11.8.0'
    implementation 'com.android.support:cardview-v7:27.0.2'
    implementation 'com.android.support:recyclerview-v7:27.0.2'
    implementation 'com.firebaseui:firebase-ui-database:3.2.1'
}

apply plugin: 'com.google.gms.google-services'

这是我的另一个 build.gradle 文件:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        classpath 'com.google.gms:google-services:3.1.1'


        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven {
            url "https://maven.google.com"
        }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

我尝试了以下操作:

  • 将buildToolsVersions从“27.0.3”更改为“27.0.2”,然后同步
  • 使缓存无效/重新启动
  • 重新构建项目

我从此处的 material.io 组件中取出导致错误的 XML 代码行。 有没有人知道我做错了什么?

谢谢 :)

8个回答

9
Material Components目前尚未通过任何官方依赖项(包括在build.gradle文件中引用的其他依赖项)发布新版本,因此27.0.2中没有包含任何最新更改,例如对Widget.MaterialComponents.TextInputLayout.OutlineBox的支持。如果你想要访问最新更改,你需要直接将Material Components库中所需部分复制到你的工程中。

谢谢!你有关于如何完成这个的指南吗?我刚开始学习。 - Richard Miller
3
很遗憾,这并不像人们希望的那样简单 - 你需要将lib目录复制到你的项目根目录下,然后编辑你的settings.gradle,将大部分代码从他们的 settings.gradle复制进去。个人建议,如果你刚开始学习,最好只使用已经包含在com.android.support:design依赖中的内容。 - ianhanniballake

8

5
如果您使用新的支持库包装,请使用以下依赖项:com.google.android.material:material:1.0.0-beta01

4

虽然跑题了,但是为了澄清Material相关的问题(对于任何一个想迁移到Material Design的人都有帮助),

将你的应用程序主题更改为继承自Material Components主题。

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- ... -->
</style>

to

<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
    <!-- ... -->
</style>

欲了解更多信息,请访问

开始使用Material组件来开发Android应用


4

添加下面的依赖项解决了我的问题。

implementation 'com.android.support:design:28.0.0'

我的所有依赖是

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.android.support:design:28.0.0'

}

2

这个样式来自设计支持库v28的alpha版本,目前还未正式发布,直到Android P发布。要使用此更改,请将compileSdkVersion更改为“android-P”,并使用此依赖项。

implementation 'com.android.support:design:28.0.0-alpha1'

1
请确保在您的AndroidManifest.xml文件中为Activity设置了materialcomponents主题。类似于以下内容:
<activity android:name=".activities.LoginActivity"
          android:screenOrientation="portrait"
          android:theme="@style/Theme.MaterialComponents.Light.NoActionBar">
</activity>

0

我遇到了同样的问题,我使用以下 dependencies 解决了它。

请使用下面的 dependencies

implementation 'com.android.support:appcompat-v7:28.0.0-alpha3'
implementation 'com.android.support:design:28.0.0-alpha3

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