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

RecyclerView子项中<View>垂直线条运行时不可见问题咨询

RecyclerView子项中垂直线条View运行时消失的解决方案

嘿,我刚好碰到过一模一样的问题!你这个垂直线条在预览里显示正常,但运行后消失,核心原因是承载它的llImageContainer这个LinearLayout的高度设置不对。

咱们看你的布局:llImageContainer的layout_height是wrap_content,它的高度完全由里面的ImageView撑起来(ImageView是固定的20dp高度)。而你给垂直线条的View设置了layout_height="match_parent",这里的match_parent是相对于llImageContainer的高度来说的——但llImageContainer的高度只有ImageView的20dp,ImageView已经占满了这个高度,所以你的垂直线条View的高度实际是0,自然就看不到了。Android Studio的预览面板会给布局默认分配一些高度,所以预览时能看到线条,运行时就没了。

给你两个靠谱的解决方案:

方案1:修改llImageContainer的高度为match_parent

直接把llImageContainer的layout_height从wrap_content改成match_parent,这样它会和外层的LinearLayout高度保持一致,垂直线条的match_parent就能撑满整个高度:

<LinearLayout
    android:id="@+id/llImageContainer"
    android:layout_width="0dp"
    android:layout_height="match_parent" <!-- 这里改成match_parent -->
    android:layout_weight="0.3"
    android:gravity="center"
    android:orientation="vertical">

    <androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/aivDay"
        android:layout_width="@dimen/_20dp"
        android:layout_height="@dimen/_20dp"
        android:layout_marginTop="@dimen/_10dp"
        android:background="@drawable/solid_circle_bg" />

    <View
        android:layout_width="@dimen/_1dp"
        android:layout_height="match_parent"
        android:background="@color/colorPrimary" />
</LinearLayout>

方案2:用ConstraintLayout重构llImageContainer(更灵活)

如果不想让llImageContainer占满整个高度,而是让线条从ImageView底部延伸到子项底部,可以把llImageContainer改成ConstraintLayout,精准控制线条的位置:

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/llImageContainer"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="0.3">

    <androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/aivDay"
        android:layout_width="@dimen/_20dp"
        android:layout_height="@dimen/_20dp"
        android:layout_marginTop="@dimen/_10dp"
        android:background="@drawable/solid_circle_bg"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <View
        android:layout_width="@dimen/_1dp"
        android:layout_height="0dp"
        android:background="@color/colorPrimary"
        app:layout_constraintTop_toBottomOf="@+id/aivDay"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="@+id/aivDay"
        app:layout_constraintEnd_toEndOf="@+id/aivDay" />
</androidx.constraintlayout.widget.ConstraintLayout>

这两种方法都能解决你的问题,我自己用方案1快速解决过类似场景,你可以试试!

内容的提问来源于stack exchange,提问作者Suhas Wagre

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 07:52:49