我无法使用 material 按钮使用 com.google.android.material.button.MaterialButton

I cant use material button using com.google.android.material.button.MaterialButton

您好,我想使用 material 按钮,例如以下链接中的按钮: https://material.io/components/buttons/#text-button

但是当我添加时:

compile group: 'com.google.android.material', name: 'material', version: '1.1.0-alpha01'

并使用:

我的代码在下面

我已经使用了许多 com.google.android.material 实现,但我的一些尝试无法做到:

implementation 'com.google.android.material:material:1.1.0-alpha09'
implementation 'com.google.android.material:material:1.1.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'com.google.android.material:material:1.0.0-alpha1'

应用gradle:


android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.esppad.fansy4"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.0'
    // https://mvnrepository.com/artifact/com.google.android.material/material
    compile group: 'com.google.android.material', name: 'material', version: '1.1.0-alpha01'

    //implementation 'com.google.android.material:material:1.1.0-alpha1'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
}

我的主要 activity 代码是:

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 android:layout_marginLeft="10dp"
 android:layout_marginRight="10dp"
 android:gravity="center">
 <com.google.android.material.button.MaterialButton
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     style="@style/Widget.MaterialComponents.Button.OutlinedButton"
     android:text="material"
     app:strokeColor="@color/colorPrimary"
     app:strokeWidth="2dp"
     android:layout_marginBottom="10dp"

     />

     <Button
         android:id="@+id/button"
         style="@style/Widget.MaterialComponents.Button.TextButton"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:text="Button" />

 <Button
     android:id="@+id/button2"

     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:text="Button" />
</LinearLayout>

我的构建 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.4.2'

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

allprojects {
    repositories {
        google()
        jcenter()

    }
}

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

代替这一行

  compile group: 'com.google.android.material', name: 'material', version: '1.1.0-alpha01'

使用这个

implementation 'com.google.android.material:material:1.1.0-alpha09'

别忘了更改您的应用主题以继承自 Material 组件主题

要将 material text buttons 添加到您的应用,请按照以下步骤操作:

implementation 'com.google.android.material:material:1.1.0-alpha09'
<com.google.android.material.button.MaterialButton
    android:id="@+id/material_text_button"
    style="@style/Widget.MaterialComponents.Button.TextButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="..."/>

Material 设计文档清楚地说明了您应该如何将库合并到您的应用程序中。您可以在 https://material.io/develop/android/docs/getting-started/.

阅读它

简而言之,您必须依赖库:

implementation 'com.google.android.material:material:<version>'

其中 <version> 是最新版本(当前为 1.1.0-alpha09)。

在此之后,您需要更新您的应用主题以使用 material 设计。转到 src/main/res/values/styles.xml 并在那里更新应用主题:

<style name="AppTheme" parent="<theme>">
   <!-- ... -->
</style>

其中 <theme> 可以是以下之一:

Theme.MaterialComponents
Theme.MaterialComponents.NoActionBar
Theme.MaterialComponents.Light
Theme.MaterialComponents.Light.NoActionBar
Theme.MaterialComponents.Light.DarkActionBar
Theme.MaterialComponents.DayNight
Theme.MaterialComponents.DayNight.NoActionBar
Theme.MaterialComponents.DayNight.DarkActionBar

最后,您可以使用任何您想要的 material 元素了。对于您的按钮:

<com.google.android.material.button.MaterialButton
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  style="@style/Widget.MaterialComponents.Button.OutlinedButton"
  android:text="material"
  app:strokeColor="@color/colorPrimary"
  app:strokeWidth="2dp"
  android:layout_marginBottom="10dp"/>

试试这个 implementation 'com.google.android.material:material:1.0.0'
要么 尝试将您的应用迁移到 androidx here is the example