failed to find style 'cardView Style' in current theme

前端 未结 4 1159
鱼传尺愫
鱼传尺愫 2020-12-29 01:22

After updating support library version 27.1.0 Android Studio unable to render CardView. It shows error message as

failed to fi         


        
相关标签:
4条回答
  • 2020-12-29 01:35

    In my case the reason of the error with CardView was because of the wrong listheader attribute of a child Spinner. Alongside the failed to find style 'cardView Style' in current theme error message I got Spinner adapter view type count must be 1, so the roots of the CardView failing were surprisingly in Spinner rendering failure.

    0 讨论(0)
  • 2020-12-29 01:47

    I also had the same problem, so I added

    <item name="cardViewStyle">@style/CardView</item>
    

    in my styles.xml file. But it was showing a warning "The resource @style/CardView is marked as private in com.android.support:design".

    So, after that I tried this one

    <item name="cardViewStyle">@style/CardView.Light</item>
    

    and this worked for me. I am using Android Studio 3.1.4 and Gradle 4.4.

    0 讨论(0)
  • 2020-12-29 01:50

    Checking the source of CardView one can see that the constructor

    public CardView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initialize(context, attrs, 0);
    }
    

    has been changed to

    public CardView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, R.attr.cardViewStyle);
    }
    

    And the new attribute cardViewStyle is defined in the library. I guess that the Android Studio preview is not including the value for this attribute from the library, for some reason. I'm not sure if it usually resolves custom attributes defined in libraries and this is a bug, or if this is intended.

    Workaround 1

    Resolve the attribute in your theme (maybe only in debug version), this way the error is gone. @style/CardView is already defined in the support library so you don't need to create the style, just reference it.

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="cardViewStyle">@style/CardView</item>
    </style>
    

    Be sure to use AppTheme in the layout preview and clean build.

    Workaround 2

    Add the style with tools namespace so that it won't affect your production code. The rendering error will be logged, but you will see the CardView anyway

    <android.support.v7.widget.CardView
        tools:style="@style/CardView"
        ...
    

    Good news

    Android Studio 3.1 seems to be handling this correctly, no workaround needed (the error in the layout preview is still logged though).

    On Android Studio 3.2 (currently in Canary channel) the error is gone.

    0 讨论(0)
  • 2020-12-29 01:51

    updating the CardView from 27.1.1 to 28.0.0-alpha3 fixed the XML preview for me on AS 3.1.3.

    implementation "com.android.support:cardview-v7:28.0.0-alpha3"
    

    it still complains, but it renders the preview.

    there's also a new androidx class now, which should be the same:

    implementation "androidx.cardview:cardview:1.0.0"
    

    when updating com.android.support:design to 28.0.0-alpha3, it hints for:

    The resource @style/CardView is marked as private in com.android.support:design
    

    downgrading back to API 27 with buildTools 27.0.3 & supportLibrary 27.1.1 also prevents the issues. might upgrade to 28.0.0 with Android Studio 3.2 then.

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