How to add cardview in layout xml in AndroidX

后端 未结 9 1069
臣服心动
臣服心动 2020-12-01 01:32

How to add cardview in layout xml in AndroidX

After upgraded to AndroidX



        
相关标签:
9条回答
  • 2020-12-01 02:10

    You have 2 different options:

    • Material Components MaterialCardView

    Just add:

    implementation 'com.google.android.material:material:1.2.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 and

    • add support for checking and dragging
    • uses a MaterialComponents theme by default (it uses by deafult the style Widget.MaterialComponents.CardView)
    • support the ShapeAppearanceModel
    • adds attributes for customizing the stroke
    0 讨论(0)
  • 2020-12-01 02:13

    For AndroidX, Add in gradle dependencies

    implementation 'androidx.cardview:cardview:1.0.0'

    Then in xml-layout use

    <androidx.cardview.widget.CardView

    0 讨论(0)
  • 2020-12-01 02:17

    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.

    0 讨论(0)
  • 2020-12-01 02:26

    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.

    0 讨论(0)
  • 2020-12-01 02:26

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

    0 讨论(0)
  • 2020-12-01 02:27

    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.



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



    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:



    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.

    0 讨论(0)
提交回复
热议问题