【问题标题】:I cant use material button using com.google.android.material.button.MaterialButton我不能使用 com.google.android.material.button.MaterialButton 使用材质按钮
【发布时间】:2019-09-03 10:32:25
【问题描述】:

您好,我想使用材料按钮,例如以下链接中的按钮: 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'

应用分级:


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'
}

我的主要活动代码是:

<?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>

我的构建等级是:

// 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
}

【问题讨论】:

    标签: android material-design android-button material-components material-components-android


    【解决方案1】:

    要将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 Theme
    • @pouyairanpour 检查更新的答案和评论。两者都有材料组件库和主题设置的链接。
    【解决方案2】:

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

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

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

    其中&lt;version&gt;是最新版本(当前为 1.1.0-alpha09)。

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

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

    其中&lt;theme&gt; 可以是以下之一:

    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
    

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

    <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"/>
    

    【讨论】:

    • 我应该如何使用风格?在应用程序主题中?你能告诉我更多吗?
    • @pouyairanpour 我编辑了答案,提供了有关主题的更多信息:)
    【解决方案3】:

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

    【讨论】:

    • 迁移到androidx是通过什么方式替代使用材质组件库的?
    【解决方案4】:

    代替这一行

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

    使用这个

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

    不要忘记将您的应用主题更改为从 Material Components 主题继承

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-05
      • 2021-01-25
      • 1970-01-01
      • 2017-05-10
      • 2022-01-10
      • 1970-01-01
      • 1970-01-01
      • 2017-07-01
      相关资源
      最近更新 更多