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

Android开发求助:RecyclerView子项CardView设置match_parent仍超出屏幕

问题原因分析

你的CardView超出屏幕的问题不是bug,是RecyclerView的布局设置有误导致的。看你activity_circular2.xml里的RecyclerView配置:你给它设置了固定宽度599dp,这会让RecyclerView在屏幕宽度小于599dp的设备上直接超出显示范围,而CardView的layout_width="match_parent"是跟随RecyclerView的宽度走的,自然就跟着超出屏幕了。

解决方案

修改activity_circular2.xml中RecyclerView的宽高设置,把固定值改成match_parent,让它自适应屏幕宽度:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".CircularAct">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
额外优化建议
  • 可以给CardView添加一点左右内边距(比如android:layout_marginHorizontal="16dp"),让内容和屏幕边缘保持距离,视觉效果更舒服:
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginHorizontal="16dp"
    android:layout_marginVertical="8dp">
    <!-- 内部TextView保持不变 -->
</androidx.cardview.widget.CardView>
  • 你的TextView用了50dp的文字大小,这个值偏大,建议改成sp单位(比如24sp),sp会跟随系统字体大小调整,适配性更好。

内容的提问来源于stack exchange,提问作者Gr8 BEAST

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.28 23:04:09