java - 如何防止wrap_content中的 View 消失

标签 java android

我遇到一个问题,当我的 View 的布局为wrap_content时,我的 View 在我的RecyclerView中消失。 RecyclerView 是两个方向的 match_parent,并使用 LinearLayout 来扩展下面的 xml 文件。 RecyclerView 的每个部分应该如下所示: What the view should look like

下面是 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/

相关文章:

android - 重新启动或恢复应用程序后如何保持微调器选择的项目

java - 在对象中设置字节字段

java - 来自服务器的响应代码

android - 具有静音、自动播放视频的 WebView 会暂停其他音频源

c# - 未设置 Xamarin Forms 文件提供程序

javascript - 有没有办法使用 android cordova 应用程序将文件上传到共享的谷歌驱动器文件夹?

java - 使 jfilechooser 显示图像缩略图

java - 严重 : java. lang.NullPointerException GAE + PrimeFaces + Spring + Maven,BookList 应用程序

java - 处理空 ArrayList 的 Android BaseAdapter

android - 如何为 ButterKnife 8 配置 ProGuard 设置?