【问题标题】:Cardview not displaying in android studio 3.5.1Cardview 未在 android studio 3.5.1 中显示
【发布时间】:2019-10-21 07:33:47
【问题描述】:

我在旧的 android studio 版本 3.3.1 上尝试了 CardView 库,并且代码可以正常工作。模拟器显示正确的图像。但是,我切换到新版本 3.5,CardView 中没有显示任何内容,甚至没有显示 TextView。

xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp"
    android:background="#fcfcfc"
    android:gravity="center"
    android:id="@+id/LinearLayout1">

    <!-- A CardView that contains a TextView -->
    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_gravity="center"
        android:layout_width="200dp"
        android:layout_height="200dp"
        card_view:cardCornerRadius="4dp">

        <TextView
            android:id="@+id/info_text"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </android.support.v7.widget.CardView>

</LinearLayout>

分级:

dependencies {

implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.android.support:cardview-v7:28.0.0'

}

【问题讨论】:

  • google 将所有 materia desine 组件替换为单个库实现 'com.google.android.material:material:1.0.0',只需导入此库并删除 cardview 库
  • @HarishReddy 这并不完全正确。还有一个 CardView 的 andriodx 实现。

标签: java android android-cardview androidx android-studio-3.5.1


【解决方案1】:

你应该使用

<androidx.cardview.widget.CardView //XML Section

添加AndroidX依赖

implementation 'androidx.cardview:cardview:1.0.0'

【讨论】:

  • 这并不完全正确。 CardView 有 2 种不同的实现。 androidx 库 (androidx.cardview.widget.CardView) 和材料组件库 (com.google.android.material.card.MaterialCardView)。如果要使用第一个,则不需要材料成分库。
  • @GabrieleMariotti 确实是先生。
【解决方案2】:

您不能同时使用 support 和 andriodx 库。

改变

implementation 'com.android.support:cardview-v7:28.0.0'

到

implementation 'androidx.cardview:cardview:1.0.0'

并在您的布局/代码中使用androidx.cardview.widget.CardView。

CardView 还有另一种实现。
它包含在Material Components Library

只需添加:

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

在你的xml中你可以使用:

 <com.google.android.material.card.MaterialCardView
  ...>

它们是不同的:

MaterialCardView 扩展 androidx.cardview.widget.CardView 并提供 CardView 的所有功能,但添加了用于自定义笔触的属性并默认使用更新的 Material 样式。

【讨论】:

    【解决方案3】:

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

    依赖{

    `def cardview_version = "1.0.0" 实现“androidx.cardview:cardview:$cardview_version” }

    和xml:

    【讨论】:

      猜你喜欢
      • 2017-05-01
      • 2017-02-13
      • 1970-01-01
      • 1970-01-01
      • 2015-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多