我正在尝试添加一条垂直线作为每个RecyclerView项目的指示器(除了正常的水平线)。用于此的普通视图由Android Studio的“布局设计器”完成。只要项目布局在设计器中显示,一切正常,但在构建应用程序后,指标在模拟器中不可见。
这是布局文件中的相关部分:
<?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 = wrap_Content和指标视图的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" />