找不到类“ com.google.android.material.button.MaterialButton”

时间:2019-04-09 05:22:44

标签: android android-theme android-design-library

我正在尝试使用新材料,做了如下更改,这是我的荣幸:

    android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "ir.ss.edu"
        minSdkVersion 14
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.android.support:support-v4:28.0.0'
    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 'android.arch.navigation:navigation-fragment:1.0.0'

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

然后我将主题更改为:

<style name="AppTheme" parent="Theme.MaterialComponents.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

当我将MaterialButton添加到布局并运行我的应用程序时,它用完了,并报错:Didn't find class "com.google.android.material.button.MaterialButton"

我该如何解决?

3 个答案:

答案 0 :(得分:1)

要使用malt

${model.relation.field}文件的com.google.android.material.button.MaterialButton下添加

dependencies

也不要同时使用build.gradleimplementation 'com.google.android.material:material:1.0.0' 的依赖关系

有关信息,请阅读此Migrating to AndroidX

答案 1 :(得分:0)

添加此依赖项

implementation 'com.google.android.material:material:1.0.0'

希望这会有所帮助

答案 2 :(得分:0)

如果您不想迁移到androidX并想继续使用设计支持库,则需要使用android.support.design.button.MaterialButton而不是com.google.android.material.button.MaterialButton

尝试使用以下代码

<android.support.design.button.MaterialButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="MATERIAL BUTTON"
    android:textSize="18sp"
    app:icon="@drawable/ic_android_white_24dp" />