【问题标题】:How can I solve this problem while creating recyclerview?创建recyclerview时如何解决这个问题?
【发布时间】:2020-07-27 18:41:04
【问题描述】:

我正在尝试用GridLayout 创建Recyclerview+ Cardview 但 Android Studio 显示错误, 我需要知道这里出了什么问题 A picture showing the error, 代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="120dp"
android:layout_height="190dp"
xmlns:cardview="http://schemas.android.com/apk/res-auto"
android:layout_margin="5dp"
cardview:cardcornerRadius="4dp">
<LinearLayout
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
<ImageView
  android:id="@+id/book_img_id"
  android:layout_width="match_parent"
  android:layout_height="160dp"
  android:scaleType="centerCrop"
  android:background="#2d2d2d"/>
<TextView
  android:id="@+id/book_title_id"
  android:textColor="#2d2d2d"
  android:textSize="13sp"
  android:gravity="center"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:text="سكر"/>
</LinearLayout>
</androidx.cardview.widget.CardView>

【问题讨论】:

  • 图片和屏幕截图可以很好地添加到帖子中,但请确保帖子在没有它们的情况下仍然清晰且有用。如果您发布代码或错误消息的图像,请确保您还复制并粘贴或直接在帖子中输入实际代码/消息。请看Why may I not upload images of code on SO when asking a question?

标签: android xml android-studio android-recyclerview android-cardview


【解决方案1】:

使用app:cardCornerRadius="4dp" 而不是cardview:cardCornerRadius="4dp"

cardCornerRadius属性在app下

尝试如下代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="120dp"
    android:layout_height="190dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:cardCornerRadius="4dp">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="160dp"
            android:src="@mipmap/ic_launcher_round"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="Android"/>
    </LinearLayout>
</androidx.cardview.widget.CardView>

希望对你有帮助

【讨论】:

  • 非常感谢,问题还是找到了
猜你喜欢
  • 2015-12-29
  • 1970-01-01
  • 2014-10-07
  • 1970-01-01
  • 2023-02-11
  • 2019-04-20
  • 2011-06-07
相关资源
最近更新 更多