You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

RecyclerView仅显示单个条目且行占满页面,设wrap_content后如何修复?

修复RecyclerView条目占满全屏仅显示一个条目的问题

嘿,我一眼就揪出问题所在了——你的行布局里犯了个常见的布局属性误用错误,咱们一步步搞定它:

问题根源

你在LinearLayout容器里给第二个TextView加了android:layout_below="@id/msg"属性,但这个属性是RelativeLayout专属的!LinearLayout完全不识别它,直接忽略了这个布局指令,导致渲染异常,最终让整个item的高度被意外拉伸到全屏。

解决方案

你有两种修复方式,选一种适合你的就行:

方式1:改用RelativeLayout作为内部容器

把行布局里的LinearLayout替换成RelativeLayout,这样layout_below属性就能正常生效,两个TextView会垂直排列:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/background">

    <TextView
        android:id="@+id/msg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/pink"
        android:text="c'est quel module"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/msg"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="10dp"
        android:text="15:12"
        android:textColor="#666"
        android:textSize="13sp" />
</RelativeLayout>

方式2:保留LinearLayout并调整属性

如果更习惯用LinearLayout,就保留它的android:orientation="vertical"设置,然后去掉无效的layout_below属性,用margin来控制两个TextView的间距:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/background"
    android:orientation="vertical"
    android:paddingVertical="10dp">

    <TextView
        android:id="@+id/msg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:background="@drawable/pink"
        android:text="c'est quel module"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="5dp"
        android:text="15:12"
        android:textColor="#666"
        android:textSize="13sp" />
</LinearLayout>

额外检查项

为了确保万无一失,再确认这两点:

  • 你的RecycleAdapter里,onCreateViewHolder要正确加载item布局:
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.your_item_layout_name, parent, false); // 替换成你的item布局文件名
        return new ViewHolder(view);
    }
    
  • 确认Message类有正确的getter方法,这样Adapter才能拿到数据显示:
    public class Message {
        private String msg;
        private String time;
    
        public Message(String msg, String time) {
            this.msg = msg;
            this.time = time;
        }
    
        public String getMsg() { return msg; }
        public String getTime() { return time; }
    }
    

做完这些修改后,你的RecyclerView应该就能正常显示所有条目,每个item的高度都会自适应内容啦!

内容的提问来源于stack exchange,提问作者Hossame Makhlof

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:57:36