Android无法解析Material Components

时间:2018-02-04 00:27:57

标签: android xml android-appcompat android-design-library android-textinputlayout

我收到此错误:

  

无法解析符号   '@风格/ 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”并同步
  • 使缓存无效/重新启动
  • 重建项目

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

谢谢:)

8 个答案:

答案 0 :(得分:9)

documentation尚未在com.android.support:design依赖项下发布新版本(或通过您可以包含在build.gradle文件中的任何其他官方依赖项),以便您提供什么&#39 ; 27.0.2中的重新查看不包含任何最近的更改 - 例如对Widget.MaterialComponents.TextInputLayout.OutlineBox的支持。

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

答案 1 :(得分:8)

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

# The first step for storing a password on disk is usually a manual one. There is nothing mandatory about the filename, but we’ll use a convention to name the file CurrentScript.ps1.credential. Given a credential that you’ve stored in the $credential variable, you can safely use the Export-CliXml cmdlet to save the credential to disk. Replace CurrentScript with the name of the script that will be loading it:
PS > $credPath = Join-Path (Split-Path $profile) 

# CurrentScript.ps1.credential
PS > $credential | Export-CliXml $credPath

# In PowerShell version 2, you must use the ConvertFrom-SecureString cmdlet:

PS > $credPath = Join-Path (Split-Path $profile) CurrentScript.ps1.credential
PS > $credential.Password | ConvertFrom-SecureString | Set-Content $credPath

答案 2 :(得分:2)

此样式来自设计支持库v28 alpha版本的部分,直到android p发布它才发布。为了使用这个更改compileSdkVersion到&#39; android-P&#39;并使用此依赖项来使用它

4.1-1.0.2.0

答案 3 :(得分:2)

如果将新包装用于支持库,则应使用:

com.google.android.material:material:1.0.0-beta01的依赖项

答案 4 :(得分:2)

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

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'

}

答案 5 :(得分:0)

我遇到了在dependencies下使用以下方法解决的相同问题

dependencies以下使用

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

答案 6 :(得分:0)

可以解决您的问题,但要澄清Material方面的内容(它可以帮助任何人迁移到Material Design简介中),

更改您的应用程序主题以从“材料组件”主题继承

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

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

有关更多信息,请访问

Getting started with Material Components for Android

答案 7 :(得分:0)

确保在您的 AndroidManifest.xml 文件中,您有一个 materialcomponents 作为您的 Activity 的主题。 类似这样的事情:

<activity android:name=".activities.LoginActivity"
          android:screenOrientation="portrait"
          android:theme="@style/Theme.MaterialComponents.Light.NoActionBar">
</activity>