RecyclerView中CardView自定义参数运行时失效问题求助
CardView自定义样式在RecyclerView中不生效的解决办法
问题情况
使用RecyclerView展示课程列表时,在lesson_item.xml中为CardView设置的cardBackgroundColor、cardCornerRadius等样式参数完全没有生效,实际显示的CardView没有圆角和自定义背景色。
预期效果:
实际运行效果:
相关代码
lesson_item.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:card_view="http://schemas.android.com/tools" android:id="@+id/lesson_card" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="20dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" card_view:cardBackgroundColor="#E8DFDF" card_view:cardCornerRadius="30dp"> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent" android:padding="4dp"> <TextView android:id="@+id/lesson_item_title" style="@style/TextAppearance.MaterialComponents.Headline4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="@string/lesson_name" android:textColor="#000000" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/lesson_item_desc" style="@style/TextAppearance.MaterialComponents.Body1" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:layout_marginTop="4dp" android:padding="10dp" android:text="@string/lesson_text" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/lesson_item_title" /> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.cardview.widget.CardView>
fragment_lessons.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFFFF" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" tools:context=".presentation.fragments.LessonsFragment"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageButton android:id="@+id/button_back_from_lessons_to_menu" android:layout_width="38dp" android:layout_height="48dp" android:background="#ED8C3E" android:contentDescription="button to back" android:src="@drawable/ic_back_24" android:textSize="24sp" android:textStyle="bold"/> <TextView android:id="@+id/hello_text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="8dp" android:paddingBottom="8dp" android:background="#ED8C3E" android:paddingStart="120dp" android:text="@string/lessons" android:textColor="#000000" android:textSize="24sp" android:textStyle="bold"/> </LinearLayout> <androidx.recyclerview.widget.RecyclerView android:id="@+id/lessons_recycler_view" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
LessonAdapter.kt
package com.example.englishease.presentation.adapter import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.TextView import androidx.cardview.widget.CardView import androidx.recyclerview.widget.RecyclerView import com.example.englishease.R class LessonAdapter(val list: List<String>, val lessonDescs: List<String>, val listener: LessonAdapter.OnItemClickListener) : RecyclerView.Adapter<LessonAdapter.MyLesson>(){ class MyLesson(item: View) : RecyclerView.ViewHolder(item) { val cardView: CardView = item.findViewById(R.id.lesson_card) } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LessonAdapter.MyLesson { val myItem = LayoutInflater.from(parent.context).inflate(R.layout.lesson_item, parent, false) return LessonAdapter.MyLesson(myItem) } override fun onBindViewHolder(holder: LessonAdapter.MyLesson, position: Int) { val lessonName = list[position] val lessonDescs = lessonDescs[position] holder.cardView.findViewById<TextView>(R.id.lesson_item_title).text = lessonName holder.cardView.findViewById<TextView>(R.id.lesson_item_desc).text = lessonDescs holder.itemView.setOnClickListener { listener.onItemClickListener(list[position]) } } override fun getItemCount() = list.size interface OnItemClickListener { fun onItemClickListener(lessonName: String) } }
解决方法
核心问题:命名空间错误
你给CardView属性用的card_view命名空间指向了http://schemas.android.com/tools,这个是Android Studio工具的命名空间,不是CardView控件的属性命名空间,导致系统无法识别这些样式参数。
修改方案
直接使用已经声明好的app命名空间(xmlns:app="http://schemas.android.com/apk/res-auto")来设置CardView的属性,把card_view:前缀替换成app:,同时删除无效的card_view命名空间声明:
修改后的lesson_item.xml头部及属性部分:
<?xml version="1.0" encoding="utf-8"?> <androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/lesson_card" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="20dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:cardBackgroundColor="#E8DFDF" app:cardCornerRadius="30dp">
额外优化建议
把fragment_lessons.xml中RecyclerView的宽高改为match_parent,避免内容被截断:
<androidx.recyclerview.widget.RecyclerView android:id="@+id/lessons_recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" />
内容的提问来源于stack exchange,提问作者StevM23
相关产品推荐
相关产品推荐

