Android无法解析Material Components

问题描述 投票:11回答:6

我收到此错误:

无法解析符号'@ 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>

这是我的app 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”并同步
  • 使高速缓存/重新启动无效
  • 重建项目

我采用了导致错误from the material.io components here的XML代码行。有谁知道我做错了什么?

谢谢 :)

android xml android-appcompat android-design-library android-textinputlayout
6个回答
9
投票

Material Components尚未在com.android.support:design依赖下发布新版本(或者通过你的build.gradle文件中可以包含的任何其他官方依赖),所以你在27.0.2中看到的不包含任何最近的变化 - 例如支持为Widget.MaterialComponents.TextInputLayout.OutlineBox

如果要访问最新更改,则必须将所需的材料组件库部分直接复制到项目中。


8
投票

这两个答案都是正确的,但我发现这个链接上的代码给出了一个错误https://codelabs.developers.google.com/codelabs/mdc-111-kotlin/#2上的OutlineBox错误拼写正确的代码是

style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"

3
投票

如果您使用新包装作为支持库,您应该使用:

com.google.android.material:material:1.0.0-beta01依赖于此


3
投票

添加以下依赖解决了我的问题

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'

0
投票

我得到了相同的问题,我在dependencies下面解决了

使用下面的dependencies

implementation 'com.android.support:appcompat-v7:28.0.0-alpha3'
implementation 'com.android.support:design:28.0.0-alpha3
© www.soinside.com 2019 - 2024. All rights reserved.