How to add cardview in layout xml in AndroidX

2020-02-08 05:17发布

How to add cardview in layout xml in AndroidX

After upgraded to 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">

gives a error

The following classes could not be found: - android.support.v7.widget.CardView (Fix Build Path, Edit XML, Create Class)

dependencies {

    implementation 'androidx.appcompat:appcompat:1.0.0'

    implementation 'androidx.cardview:cardview:1.0.0'

but I don't know how to use CardView in xml under AndroidX

Thank you

8条回答
ら.Afraid
2楼-- · 2020-02-08 05:21

The class has changed to:

androidx.cardview.widget.CardView

If you want to use the old class names, you need to add android:enableJetifier=true to your gradle.properties and use the appcompat dependencies instead.

查看更多
兄弟一词,经得起流年.
3楼-- · 2020-02-08 05:22

I think the Best Approach should be like, understanding the problem statement first.

Scenario:

  • Search for the library which Google Inc has changed for "AndroidX" and include in project

Solution :

Go to Artifact Mappings page of Android Developer Site and Search for your desired library.

enter image description here

Now go to AndroidX Releases page and check for the current version number

enter image description here

now go to build.gradle[module.app] add the library you want. (ie. CardView in my case) and press
Sync Now and necessary files will be added to your project by gradle.

See the image below:

enter image description here

Now in xml file you should get CardView or something you have just imported.

NB: pictures included here are (the first two) taken from Android Developer's Page which belongs to Google Inc and could be different depending on the time you see. See this post's posting time.

查看更多
啃猪蹄的小仙女
4楼-- · 2020-02-08 05:23

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

to

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

it is download all design related class. its work for Me

查看更多
我命由我不由天
5楼-- · 2020-02-08 05:26

Add 'androidx.cardview:cardview:1.0.0' in gradle dependencies and modify xml accordingly.

查看更多
爷的心禁止访问
6楼-- · 2020-02-08 05:29

You have 2 different options:

Just add:

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

and in your xml you can use:

 <com.google.android.material.card.MaterialCardView
  ...>
  • Androidx CardView

Just add:

implementation 'androidx.cardview:cardview:1.0.0'

and in your xml you can use:

<androidx.cardview.widget.CardView>

They are different:

MaterialCardView extends androidx.cardview.widget.CardView and provides all of the features of CardView, but adds attributes for customizing the stroke and uses an updated Material style by default (it uses by deafult the style Widget.MaterialComponents.CardView)

More info here.

查看更多
放我归山
7楼-- · 2020-02-08 05:32

Replace this line

from

<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"

This will work fine in AndroidX.

查看更多
登录 后发表回答