在XML中添加Material Button导致应用崩溃

use*_*100 2 user-interface android button material-design android-design-library

当我将以下代码添加到layout.xml文件时,加载布局时应用程序崩溃。

码:

<android.support.design.button.MaterialButton
    android:id="@+id/b_p_add"
    android:layout_width="143dp"
    android:layout_height="38dp"
    android:layout_below="@+id/et_p_addl_notes"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="43dp" />
Run Code Online (Sandbox Code Playgroud)

我检查了我的应用程序gradle文件并发现

implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support:support-v4:28.0.0'
Run Code Online (Sandbox Code Playgroud)

因此,存在所需的设计,支持和appcompat依赖项。

注意:除上述代码外,未添加其他代码,即,如果我未在XML中添加“材质”按钮,则该应用程序运行正常,如果在XML中添加了“材质”按钮,则该应用程序将崩溃。

Sha*_*jum 5

请从以下位置更改您的应用主题:

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

<style name="AppTheme" parent="Theme.MaterialComponents.Light">

为了使MaterialButton正常工作,您的应用程序主题必须继承自Theme.MaterialComponents(或子孙),希望对您有所帮助。