【问题标题】:How to add cardview in layout xml in AndroidX如何在AndroidX的布局xml中添加cardview
【发布时间】:2019-04-04 23:45:36
【问题描述】:

如何在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(修复构建路径、编辑 XML、创建类)

dependencies {

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

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

谢谢

【问题讨论】:

    标签: android androidx


    【解决方案1】:
     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]

    【讨论】:

    • implementation 'androidx.cardview:cardview:1.0.0' 必须添加这个
    【解决方案2】:

    您有两种不同的选择:

    只需添加:

    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 的所有功能和

    • 添加对checkingdragging 的支持
    • 默认使用MaterialComponents主题(默认使用Widget.MaterialComponents.CardView风格)
    • 支持ShapeAppearanceModel
    • 为自定义笔画添加属性

    【讨论】:

    • 很抱歉,您发布的所有参考链接现在都无效。你的答案对当前的 android 版本是否正确?
    • @nynohu 感谢您的反馈。链接刚刚修复。是的,两者都适用于当前的 android 版本。
    • @GabrieleMariotti 谢谢你。文档和代码实验室现在似乎与 4.0.1 非常不同步。现在尝试学习这些东西简直就是一场噩梦。
    【解决方案3】:

    变化: 1。 build.gradle(:app)

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

    2。在 xml 部分:

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

    【讨论】:

      【解决方案4】:

      我在 Android Studio 3.5.1 中遇到了同样的问题。它对我有用:

      def cardview_version = "1.0.0"
      implementation "androidx.cardview:cardview:$cardview_version"
      
      
      xmln: <androidx.cardview.widget.CardView>....
      

      【讨论】:

        【解决方案5】:

        对于 AndroidX,添加 gradle 依赖项

        implementation 'androidx.cardview:cardview:1.0.0'

        然后在xml-layout中使用

        &lt;androidx.cardview.widget.CardView

        【讨论】:

          【解决方案6】:

          我认为Best Approach应该是,首先理解问题陈述。

          场景:

          • 搜索 Google Inc 为“AndroidX”更改的库并包含在项目中

          解决方案:

          转到 Android 开发者网站的 Artifact Mappings 页面并搜索您想要的库。



          现在转到 AndroidX Releases 页面并检查当前版本号


          现在转到 build.gradle[module.app] 添加您想要的库。 (即在我的情况下为 CardView)并按
          Sync Now,必要的文件将通过 gradle 添加到您的项目中。

          请参见下图:



          现在在 xml 文件中你应该得到 CardView 或者你刚刚导入的东西。

          注意:此处包含的图片(前两张)取自属于 Google Inc 的Android Developer's Page,可能会因您看到的时间而有所不同。查看此帖子的发布时间。

          【讨论】:

          • 很好的答案 - 解释得很好。
          • 很好解释的答案。谢谢
          【解决方案7】:

          implementation com.google.android.material:material:1.0.0

          implementation com.google.android.material:material:1.0.0-alpha1

          它是下载所有设计相关的课程。 它为我工作

          【讨论】:

            【解决方案8】:

            替换此行

            来自

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

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

            这在 AndroidX 中可以正常工作。

            【讨论】:

            • @ugali soft 在问题描述中,已经添加了“implementation 'androidx.cardview:cardview:1.0.0'”。因此,不需要再次添加此依赖项。
            • 是的,当然,这是我第一次感到困惑,我直接回答了这个答案,没有添加依赖。
            • @ugali soft 注意。
            【解决方案9】:

            在 gradle 依赖项中添加 'androidx.cardview:cardview:1.0.0' 并相应地修改 xml。

            【讨论】:

            • 提问者已将 'androidx.cardview:cardview:1.0.0' 添加到其依赖项中,如问题中所述。
            • 但是提到这件事对我很有帮助,因为我忘记了
            • @JerryChong:但是提问者在 xml 文件中没有改变。在那里,他在 xml Tag 中提到了“android.support.v7.widget.CardView”。
            • 我已经尝试了大多数答案,这个对我有用
            【解决方案10】:

            班级已更改为:

            androidx.cardview.widget.CardView
            

            如果您想使用旧的类名,您需要将 android:enableJetifier=true 添加到您的 gradle.properties 并改用 appcompat 依赖项。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2012-12-04
              • 2023-03-14
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多