如何在AndroidX中的布局XML中添加卡片视图(CardView)。

73

如何在AndroidX的布局xml中添加CardView

升级到AndroidX后

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp">

出现错误

无法找到以下类: - android.support.v7.widget.CardView (请修复Build Path、编辑XML或创建该类)

dependencies {

    implementation 'androidx.appcompat:appcompat:1.0.0'
    
    implementation 'androidx.cardview:cardview:1.0.0'

但是我不知道如何在AndroidX下使用XML中的CardView

谢谢

10个回答

113

课程已更改为:

androidx.cardview.widget.CardView
如果您想使用旧的类名,需要在gradle.properties中添加android:enableJetifier=true并使用appcompat依赖项。

44

我认为“最佳方法”应该是先了解问题陈述。

场景:

  • 搜索谷歌公司已更改为“AndroidX”的库,并在项目中包含它

解决方案:

前往 Android 开发者网站的工件映射页面,搜索您需要的库。

输入图像描述

现在转到AndroidX 发布版页面,检查当前版本号

输入图像描述

然后转到 build.gradle[module.app] 添加您想要的库。(例如,在我的情况下为 CardView),然后按
Sync Now,gradle 将向您的项目添加必要的文件。

请参见以下图像:

输入图像描述

现在,您应该在 xml 文件中获得刚导入的 CardView 或其他内容。

NB: 此处包含的图片(前两张)取自于属于 Google 公司的 Android 开发者页面,根据您查看的时间可能会有所不同。请参见此帖子发布的时间。


1
优秀的答案 - 解释得很清楚。 - Brian Douglas Moakley
讲解得非常清楚。谢谢。 - MaKi

29

您有两个不同的选择:

只需添加:

implementation 'com.google.android.material:material:1.2.0'

在你的XML中,你可以使用:

 <com.google.android.material.card.MaterialCardView
  ...>
  • Androidx CardView(卡片视图)

只需添加:

implementation 'androidx.cardview:cardview:1.0.0'

在你的xml中,你可以使用:

<androidx.cardview.widget.CardView>

它们是不同的:

MaterialCardView 继承自 androidx.cardview.widget.CardView 并提供了 CardView 的所有功能以及

  • 添加支持勾选拖动
  • 默认使用一个 MaterialComponents 主题(默认使用样式 Widget.MaterialComponents.CardView
  • 支持 ShapeAppearanceModel
  • 添加了定制描边的属性

很抱歉,您发布的所有参考链接现在都已失效。您的答案是否适用于当前的Android版本? - nynohu
@nynohu 感谢您的反馈。链接已经修复。是的,两个链接都可以在当前的安卓版本上使用。 - Gabriele Mariotti
@GabrieleMariotti 谢谢你提供这个。文档和codelabs似乎与4.0.1极不同步。这让现在学这些东西成为一场噩梦。 - Paula Livingstone

28

对于AndroidX,请在gradle依赖项中添加

implementation 'androidx.cardview:cardview:1.0.0'

然后在XML布局中使用

<androidx.cardview.widget.CardView


13
在gradle的依赖中添加'androidx.cardview:cardview:1.0.0',并相应修改XML。

提问者已经按照问题中提到的方式将'androidx.cardview:cardview:1.0.0'添加到其依赖项中。 - Jerry Chong
但是提到这件事情对我帮助很大,因为我已经忘记了。 - Noor Hossain
@JerryChong:但是问题提出者在XML文件中没有进行更改。他在XML标签中提到了“android.support.v7.widget.CardView”。 - Rakesh Kumar
我尝试了大多数答案,这个对我起作用了。 - Anubhav Pandey

5

请替换此行

<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"

to

<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"

这将在AndroidX中正常运行。

@ugali软件 在问题描述中已经添加了“implementation 'androidx.cardview:cardview:1.0.0'” ,因此不需要再次添加此依赖项。 - Jerry Chong
当然,一开始它让我感到困惑,我直接查看了这个答案,而没有添加依赖项。 - ugali soft
@ugali软件 已经注意到了。 - Jerry Chong

2

实现 com.google.android.material:material:1.0.0

变更为

实现 com.google.android.material:material:1.0.0-alpha1

这将下载所有与设计相关的类。 对我很有效。


1

我在Android Studio 3.5.1中遇到了同样的问题。以下方法对我有效:

def cardview_version = "1.0.0"
implementation "androidx.cardview:cardview:$cardview_version"


xmln: <androidx.cardview.widget.CardView>....

0

更改内容: 1. build.gradle(:app)

dependencies {
...
implementation 'androidx.cardview:cardview:1.0.0' 
....
}

2. 在 XML 部分:

<androidx.cardview.widget.CardView>
...
</androidx.cardview.widget.CardView>

0
 implementation 'androidx.cardview:cardview:1.0.0' // must add this ``


               <androidx.cardview.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:layout_marginLeft="25dp"
                android:layout_marginTop="20dp"
                android:id="@+id/game_id"
                android:layout_marginRight="25dp"
                app:cardBackgroundColor=""
                app:cardCornerRadius="15dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:orientation="horizontal"
                    android:layout_height="match_parent">

                    <ImageView
                        android:layout_width="130dp"
                        android:layout_height="170dp"
                        android:layout_gravity="center"
                        android:layout_marginLeft="5dp"
                        android:background="@drawable/gamess">

                    </ImageView>


                    <TextView
                        android:layout_width="130dp"
                        android:layout_marginLeft="20dp"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/game"
                        android:layout_centerHorizontal="true"
                        android:layout_marginTop="10dp"
                        android:gravity="center"
                        android:text="Gamensjnjdhddgsgddgjsdjsdgjsgdjsgddgjsdjsdjg"
                        android:textColor="@color/white_color"
                        android:textSize="16dp"
                        android:textStyle="bold">

                    </TextView>
                </LinearLayout>

            </androidx.cardview.widget.CardView>

[![Blockquote][1]][1]


实现 'androidx.cardview:cardview:1.0.0' 必须添加此内容。 - Aqif

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接