MaterialCardView crashes with material:1.1.0

后端 未结 7 1718
青春惊慌失措
青春惊慌失措 2021-01-01 12:15

I am using MaterialCardView as my cardview layout. Now, Android-studio started showing me that instead of current implementation \"com.google.android.mat

相关标签:
7条回答
  • 2021-01-01 12:34

    The error is in the stacktrace:

    Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).

    With the version 1.1.0 you have to inherit from a Theme.MaterialComponents.

    Change your app theme to

    <style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
      <!-- ....  -->
    </style>
    
    0 讨论(0)
  • 2021-01-01 12:36

    It works after changing this <style name="AppTheme" parent="Theme.AppCompat.DayNight> to <style name="AppTheme" parent="Theme.MaterialComponents.DayNight">

    0 讨论(0)
  • 2021-01-01 12:40

    you can just keep it in your materialcardview itself also:

     <com.google.android.material.card.MaterialCardView
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          app:cardCornerRadius="8dp"
         **** android:theme="@style/Theme.MaterialComponents.Light"  *****
          app:cardElevation="8dp">
    
    0 讨论(0)
  • 2021-01-01 12:43

    In my case I just added the android:theme="" attribute and it worked. My Material Card View element is as below -

    <com.google.android.material.card.MaterialCardView
                android:id="@+id/edit_profile_card"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginStart="@dimen/dp_16"
                android:layout_marginTop="@dimen/dp_10"
                android:layout_marginEnd="@dimen/dp_16"
                android:layout_marginBottom="@dimen/dp_10"
                **android:theme="@style/Theme.MaterialComponents.DayNight"**
                app:cardCornerRadius="@dimen/dp_10"
                app:cardElevation="@dimen/dp_20"
                app:state_dragged="false">
    
               ....
               ....
    
            </com.google.android.material.card.MaterialCardView>
    
    0 讨论(0)
  • 2021-01-01 12:48

    You need to change your AppTheme to have it's parent theme as a descendant of MaterialComponent

    In your particular case changing the AppTheme from

      <style name="AppTheme" parent="Theme.AppCompat.DayNight">
    

    to

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

    should fix the issue.

    0 讨论(0)
  • 2021-01-01 13:00

    If after changing the theme to Theme.MaterialComponents in the styles.xml didn't work then make sure you have added style tag in the MaterialCard.

    style="@style/Widget.MaterialComponents.CardView"
    
    0 讨论(0)
提交回复
热议问题