RecyclerView 中的彩色线条

Colored lines in RecyclerView

我正在尝试为每个 RecyclerView 项目(除了正常的水平项目)添加一条垂直线作为指示器。正如 Android Studio 的 "Layout Designer" 所做的那样,使用了一个普通视图。只要项目布局在设计器中显示,一切正常,但构建应用程序后在模拟器中看不到指示器。

这是布局文件中的相关部分:

<?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:focusable="true"
    android:padding="@dimen/myDefault"
    android:clickable="true"
    android:focusableInTouchMode="true"
    android:orientation="vertical" >

    <View
        android:id="@+id/indicator"
        android:layout_width="2dp"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="#0000ff" />

    <TextView
        android:id="@+id/entry"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:paddingLeft="4dp"
        android:paddingStart="4dp" />

</RelativeLayout>

如何解决这个问题?

此行为的原因是 RelativeLayout 的 height=wrapContent 与指示器视图的 height=matchParent 之间存在冲突。

像这样更改您的指示器视图,以从具有实际高度的入口视图获取其高度,该高度由其文本内容定义。

<View
    android:id="@+id/indicator"
    android:layout_width="2dp"
    android:layout_height="match_parent"
    android:layout_alignParentStart="true"
    android:layout_alignParentLeft="true"
    android:layout_alignTop="@+id/entry"
    android:layout_alignBottom="@+id/entry"
    android:background="#0000ff" />