RecyclerView列表项内容无法完整显示问题求助
Android RecyclerView列表项内容无法完整显示问题修复
问题现象
使用RecyclerView展示列表时,单个列表项布局已完成,但内容无法完整显示:小图被截断,其下方所有文字均未显示。
原布局代码
<?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" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.cardview.widget.CardView android:clickable="true" android:layout_width="match_parent" android:layout_height="match_parent" android:elevation="6dp" android:layout_marginBottom="16dp" android:padding="10dp" app:cardUseCompatPadding="true"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:layout_marginStart="10dp" android:layout_marginEnd="16dp" android:padding="10dp"> <ImageView android:id="@+id/rv_list_main_img" android:layout_width="match_parent" android:layout_height="200dp" android:scaleType="centerCrop" android:src="@drawable/longtime"></ImageView> <ImageView android:id="@+id/main_list_rv_sender_img" android:background="@drawable/imageborder" android:padding="10dp" android:scaleType="fitXY" android:src="@drawable/shorttime" android:layout_width="100dp" android:layout_height="100dp"></ImageView> <TextView android:id="@+id/main_list_rv_sender_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Abador" android:layout_marginTop="16dp" android:fontFamily="@font/baumans" android:textSize="23sp" android:textStyle="bold"></TextView> <TextView android:id="@+id/main_list_rv_place" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Bella Vista, PY" android:textSize="16sp"></TextView> <TextView android:id="@+id/main_list_rv_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:gravity="bottom" android:text="600 USD/mtl" android:textSize="23sp" android:textStyle="bold"></TextView> <TextView android:id="@+id/main_list_rv_desc" android:layout_width="260dp" android:layout_height="60dp" android:layout_alignParentLeft="true" android:gravity="left" android:maxLength="200" android:maxLines="2" android:text="Beschreibung Haus sdf sdfe Beschreibung Haus sdf sdfe" android:textSize="17sp" android:textStyle="bold"></TextView> <ImageButton android:id="@+id/main_list_rv_message_btn" android:layout_width="60dp" android:layout_height="60dp" android:layout_alignParentRight="true" android:backgroundTint="@color/white" android:scaleType="centerCrop" android:src="@drawable/send"></ImageButton> </LinearLayout> </androidx.cardview.widget.CardView> </LinearLayout>
效果对比
预期显示效果:

实际显示效果:

问题原因及修复方案
问题根源
布局中最外层LinearLayout、CardView以及内部LinearLayout都设置了android:layout_height="match_parent",强制容器占满父布局高度,当内部内容总高度超过该值时,超出部分会被截断。
修复步骤
将以下三个容器的layout_height从match_parent改为wrap_content,让容器根据内部内容自适应高度:
- 最外层LinearLayout
- CardView
- 内部嵌套的LinearLayout
同时,原布局中TextView和ImageButton使用的layout_alignParentLeft、layout_alignParentRight是RelativeLayout专属属性,在LinearLayout中无效,需删除。
修改后的布局代码
<?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" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <!-- 修改为wrap_content --> <androidx.cardview.widget.CardView android:clickable="true" android:layout_width="match_parent" android:layout_height="wrap_content" <!-- 修改为wrap_content --> android:elevation="6dp" android:layout_marginBottom="16dp" android:padding="10dp" app:cardUseCompatPadding="true"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" <!-- 修改为wrap_content --> android:orientation="vertical" android:layout_marginStart="10dp" android:layout_marginEnd="16dp" android:padding="10dp"> <ImageView android:id="@+id/rv_list_main_img" android:layout_width="match_parent" android:layout_height="200dp" android:scaleType="centerCrop" android:src="@drawable/longtime"></ImageView> <ImageView android:id="@+id/main_list_rv_sender_img" android:background="@drawable/imageborder" android:padding="10dp" android:scaleType="fitXY" android:src="@drawable/shorttime" android:layout_width="100dp" android:layout_height="100dp"></ImageView> <TextView android:id="@+id/main_list_rv_sender_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Abador" android:layout_marginTop="16dp" android:fontFamily="@font/baumans" android:textSize="23sp" android:textStyle="bold"></TextView> <TextView android:id="@+id/main_list_rv_place" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Bella Vista, PY" android:textSize="16sp"></TextView> <TextView android:id="@+id/main_list_rv_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:gravity="bottom" android:text="600 USD/mtl" android:textSize="23sp" android:textStyle="bold"></TextView> <TextView android:id="@+id/main_list_rv_desc" android:layout_width="260dp" android:layout_height="60dp" android:gravity="left" android:maxLength="200" android:maxLines="2" android:text="Beschreibung Haus sdf sdfe Beschreibung Haus sdf sdfe" android:textSize="17sp" android:textStyle="bold"></TextView> <ImageButton android:id="@+id/main_list_rv_message_btn" android:layout_width="60dp" android:layout_height="60dp" android:backgroundTint="@color/white" android:scaleType="centerCrop" android:src="@drawable/send"></ImageButton> </LinearLayout> </androidx.cardview.widget.CardView> </LinearLayout>
内容的提问来源于stack exchange,提问作者Captai-N
相关产品推荐
相关产品推荐

