如何解决CardView错误:无法解析类android.support.v7.widget.CardView
我正在尝试在我的recyclerview中使用cardview。回收者视图是独立运行的。我已经设置了数据绑定。但是我希望recyclerview看起来更好,这就是为什么我要使用cardview的原因。
这是我用于各个视图的布局文件:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="individualEntry"
type="com.zebotek.balancedmindjournal.persistance.JournalEntryDataClass" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
card_view:cardCornerRadius="4dp">
<TextView
android:id="@+id/dateTextBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
app:dateStringFormatted="@{individualEntry}"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/journalEntryTextBox"
android:layout_width="match_parent"
android:layout_height="36dp"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:textSize="14sp"
app:journalEntryText="@{individualEntry}"
app:layout_constraintTop_toBottomOf="@+id/dateTextBox" />
</android.support.v7.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
这是我的依赖项:
cardView_version = '28.0.0'
implementation "com.android.support:cardview-v7:$cardView_version"
implementation "com.android.support:appcompat-v7:$cardView_version"
implementation "com.android.support:recyclerview-v7:$cardView_version"
我无法确定我可以做的任何事情。我的minSdkVersion设置为21。
如果没有cardview,代码也可以正常工作。如果我添加cardview,现在它将无法编译,并且出现该错误。我什至尝试将cardview放置在constraintlayout之上。我用cardview替换了constraintlayout,甚至用cardview替换了顶层布局。
解决方法
尝试使用
androidx.cardview.widget.CardView
,而不是android.support.v7.widget.CardView
。
并添加dependency:
implementation "androidx.cardview:cardview:1.0.0"
,
如果您使用的是androidx(在您的第一个布局中可以看到),我仍然建议使用AndroidX的CardView
只需导入您的build.gradle
dependencies {
implementation "androidx.cardview:cardview:1.0.0"
implementation "androidx.appcompat:appcompat:1.2.0"
implementation "androidx.recyclerview:recyclerview:1.1.0"
}
另一建议是删除ConstraintLayout并仅保留CardView
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。