failed to find style 'cardView Style' in current theme

前端 未结 4 1186
鱼传尺愫
鱼传尺愫 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: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.

    
    
    

    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

    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.

提交回复
热议问题