我遇到一个问题,当我的 View 的布局为wrap_content时,我的 View 在我的RecyclerView中消失。 RecyclerView 是两个方向的 match_parent,并使用 LinearLayout 来扩展下面的 xml 文件。 RecyclerView 的每个部分应该如下所示:
下面是 XML:
<?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:layout_margin="10dp">
<View
android:id="@+id/scheduleBlockColor"
android:layout_width="15dp"
android:layout_height="match_parent"
android:layout_alignBottom="@+id/scheduleBlockText"
android:background="@color/colorCougarRed" />
<RelativeLayout
android:id="@+id/scheduleBlockText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/scheduleBlockColor"
android:layout_toRightOf="@id/scheduleBlockColor"
android:paddingEnd="0dp"
android:paddingLeft="5dp"
android:paddingRight="0dp"
android:paddingStart="5dp">
<TextView
android:id="@+id/scheduleBlockTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp"
android:text="@string/schedule_block_time_default"
android:textSize="16sp" />
<TextView
android:id="@+id/scheduleBlockClassName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/scheduleBlockTime"
android:ellipsize="end"
android:padding="2dp"
android:singleLine="true"
android:text="@string/schedule_block_class_default"
android:textColor="#000000"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/scheduleBlockRoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/scheduleBlockClassName"
android:padding="2dp"
android:text="@string/schedule_block_room_default"
android:textSize="16sp" />
</RelativeLayout>
</RelativeLayout>
我的问题是,当 RecyclerView 加载时,如何防止彩色部分消失?如果我将上面的父relativelayout设置为match_parent,它就可以正常工作。我试过this answer ,但没有取得同样的成功
最佳答案
这是因为<View>
上设置的高度。由于它没有内容,它会变得困惑(即使使用 match_parent
)。由于目的是具有垂直条纹,因此您可以将其固定到父级的顶部和底部。您已经在底部执行此操作(类似于将其与 TextView 容器的底部对齐),因此您只需要处理顶部锚定:
<View
android:id="@+id/scheduleBlockColor"
...
android:layout_alignParentTop="true"
... />
关于java - 如何防止wrap_content中的 View 消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39280078/