Using Material Button gives ClassNotFound runtime error

前端 未结 6 2144
温柔的废话
温柔的废话 2020-12-30 00:50

I am in the process of replacing the buttons in my app with Material Buttons using in the XML file and

相关标签:
6条回答
  • 2020-12-30 01:08

    In my opinion, the best choice is to create a separate own style to the material button where it extends the material theme:

    <style name="MatButton" parent="Theme.MaterialComponents">
        <item name="colorOnPrimary">@android:color/holo_red_light</item>
        <item name="colorPrimary">@android:color/holo_orange_dark</item>
        <item name="colorOnSurface">@android:color/holo_orange_light</item>
    </style>
    
    <com.google.android.material.button.MaterialButton
        android:id="@+id/searchBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Search"
        android:textColor="@android:color/white"
        android:textAppearance="@style/TextAppearance.MaterialComponents.Button"
        android:theme="@style/MatButton"
        />
    
    0 讨论(0)
  • 2020-12-30 01:08

    if you are using material theme component in your app, then try this.

    make changes in your styles.xml from before to after ->

    before:

    <resources>
        <!-- Base application theme. -->
        <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
            <!-- Customize your theme here. -->
            <item name="colorPrimary">@color/colorPrimary</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="colorAccent">@color/colorAccent</item>
        </style>
    </resources>
    

    after:

    <resources>
        <!-- Base application theme. -->
        <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
            <!-- Customize your theme here. -->
            <item name="colorPrimary">@color/colorPrimary</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="colorAccent">@color/colorAccent</item>
        </style>
    </resources>
    
    0 讨论(0)
  • 2020-12-30 01:12

    As it suggest HERE you have to add a dependency in your build.gradle:

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

    Or if you already use google support design library you must change your app theme to inherit from a Material Components theme

    <style name="Theme.MyApp" parent="Theme.MaterialComponents.Light">
    <!-- ... -->
    

    If you cannot change your theme to inherit from a Material Components theme, you can inherit from a Material Components Bridge theme.

    <style name="Theme.MyApp" parent="Theme.MaterialComponents.Light.Bridge">
    <!-- ... -->
    

    How Reyske said:

    note that it is out of beta now! So you can use implementation 'com.google.android.material:material:1.0.0'

    0 讨论(0)
  • 2020-12-30 01:16

    When crashing The following message was output to Logcat.

    Caused by: java.lang.IllegalArgumentException: This component requires that you specify a valid TextAppearance attribute. Update your app theme to inherit from Theme.MaterialComponents (or a descendant).

    Therefore, you can also display MaterialButton by adding TextAppearance.

    <com.google.android.material.button.MaterialButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@android:string/ok"
        android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
    
    0 讨论(0)
  • 2020-12-30 01:16

    You should use textAppearance and your font style instead of fontFamily & textSize

    0 讨论(0)
  • 2020-12-30 01:24

    Try to update your app theme to inherit from Theme.MaterialComponents (or a descendant)

    0 讨论(0)
提交回复
热议问题