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

新手上路,请多包涵

如何在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

谢谢

原文由 Jack Wilson 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 660
2 个回答

班级已更改为:

 androidx.cardview.widget.CardView

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

原文由 TheWanderer 发布,翻译遵循 CC BY-SA 4.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]

原文由 Aqif 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题