【问题标题】:Android cannot resolve Material ComponentsAndroid 无法解析 Material 组件
【发布时间】:2018-07-14 04:25:34
【问题描述】:

我收到此错误:

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

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

谢谢你:)

【问题讨论】:

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


    【解决方案1】:

    确保在您的 AndroidManifest.xml 文件中,您有一个 materialcomponents 作为 Activity 的主题。 像这样的东西:

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

    【讨论】:

      【解决方案2】:

      不知何故,你的问题,但澄清材料的东西(它可以帮助任何人迁移到材料设计介绍),

      将您的应用主题更改为从 Material Components 主题继承

      <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

      【讨论】:

        【解决方案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'
        
        }
        

        【讨论】:

          【解决方案4】:

          我在dependencies 下遇到了同样的问题

          dependencies下方使用

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

          【讨论】:

            【解决方案5】:

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

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

            【讨论】:

              【解决方案6】:

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

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

              【讨论】:

                【解决方案7】:

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

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

                【讨论】:

                  【解决方案8】:

                  Material Components 尚未在 com.android.support:design 依赖项下发布新版本(或通过您可以包含在 build.gradle 文件中的任何其他官方依赖项),所以您在 27.0.2 中看到的内容确实如此不包含任何最近的更改 - 例如对 Widget.MaterialComponents.TextInputLayout.OutlineBox 的支持。

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

                  【讨论】:

                  • 谢谢!你有如何做到这一点的指南吗?我才刚刚开始
                  • 不幸的是,它并不像人们想的那么简单——您需要将lib directory 复制到项目的根目录中,然后将settings.gradle 编辑到大部分代码中来自their settings.gradle。就个人而言,我可能会坚持使用已经作为 com.android.support:design 依赖项的一部分启动的东西,特别是如果你刚刚开始。
                  猜你喜欢
                  • 2019-10-25
                  • 2018-09-14
                  • 2021-10-24
                  • 2017-10-17
                  • 2021-11-16
                  • 1970-01-01
                  • 2021-05-05
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多