如果我将Recycler View子项高度的简单视图设置为match_parent,它将变为不可见

时间:2017-10-28 13:24:27

标签: android android-layout android-recyclerview

我的RecyclerView项目布局左侧有一个简单的视图,看起来像这样

<View
    android:id="@+id/status_view"
    android:layout_width="5dp"
    android:layout_height="match_parent"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:background="@color/pending_color_my_schedule" />

我需要像他这样的父母(RelativeLayout)的全高度

enter image description here

父布局代码

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/my_schedule_item_layout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_marginLeft="@dimen/my_schedule_item_margins"
      android:layout_marginRight="@dimen/my_schedule_item_margins"
      android:background="@drawable/shadow_212838"/>

But when I set my `View` to `match_parent's` height it becomes invisible and I can't find a clue why.

If I set it to 150dp or 200dp it works, but I need it to be flexible as the height of parent.

Entire XML file (UPD)

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/my_schedule_item_layout"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_marginLeft="@dimen/my_schedule_item_margins"
  android:layout_marginRight="@dimen/my_schedule_item_margins"
  android:layout_centerHorizontal="true"
  android:layout_centerVertical="true"
  android:background="@drawable/shadow_212838">

  <TextView
    android:id="@+id/time_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/my_schedule_text_margins"
    android:layout_marginBottom="@dimen/my_schedule_text_margins_small"
    android:layout_marginStart="@dimen/my_schedule_text_margins_start"
    android:layout_marginEnd="@dimen/my_schedule_text_margins"
    android:textColor="@color/black_standart"
    android:textSize="20sp"
    tools:text="Tomorrow: 11:45 - 15:00" />

  <TextView
    android:id="@+id/status_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/my_schedule_text_margins"
    android:layout_marginBottom="@dimen/my_schedule_text_margins_small"
    android:layout_marginEnd="@dimen/my_schedule_text_margins"
    android:layout_alignParentEnd="true"
    android:textSize="12sp"
    tools:text="Pending" />

  <ImageView
    android:id="@+id/status_color_image"
    android:layout_width="@dimen/my_schedule_item_status_size"
    android:layout_height="@dimen/my_schedule_item_status_size"
    android:layout_marginTop="@dimen/my_schedule_status_icon_top_padding"
    android:layout_marginEnd="@dimen/my_schedule_text_margins_small"
    android:layout_toStartOf="@id/status_text"
    android:background="@drawable/pending_circle" />

  <TextView
    android:id="@+id/location_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/my_schedule_text_margins_small"
    android:layout_marginBottom="@dimen/my_schedule_text_margins_small"
    android:layout_marginEnd="@dimen/my_schedule_text_margins_small"
    android:layout_alignStart="@id/time_text"
    android:layout_below="@id/time_text"
    android:drawablePadding="@dimen/my_schedule_text_margins_small"
    android:drawableStart="@drawable/ic_location_grey"
    android:textSize="12sp"
    tools:text="Factory and Co Aeroville" />

  <TextView
    android:id="@+id/function_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/my_schedule_text_margins_small"
    android:layout_marginEnd="@dimen/my_schedule_text_margins_small"
    android:layout_alignStart="@id/time_text"
    android:layout_below="@id/location_text"
    android:paddingBottom="@dimen/my_schedule_text_margins_start"
    android:textSize="14sp"
    tools:text="Function: Manager" />

  <Button
    android:id="@+id/decline_button"
    android:layout_width="@dimen/my_schedule_item_button_width"
    android:layout_height="@dimen/my_schedule_item_button_height"
    android:layout_marginBottom="@dimen/my_schedule_item_buttons_bottom_padding"
    android:layout_alignStart="@id/function_text"
    android:layout_below="@id/function_text"
    android:background="@drawable/blue_transparent_btn_bg"
    android:text="@string/decline"
    android:textColor="@color/clear_blue" />

  <Button
    android:id="@+id/confirm_button"
    android:layout_width="@dimen/my_schedule_item_button_width"
    android:layout_height="@dimen/my_schedule_item_button_height"
    android:layout_marginStart="@dimen/my_schedule_item_buttons_bottom_padding"
    android:layout_below="@id/function_text"
    android:layout_toEndOf="@id/decline_button"
    android:background="@drawable/blue_solid_button_bg"
    android:text="@string/confirm"
    android:textColor="@color/white_standart" />
  <View
    android:id="@+id/status_view"
    android:layout_width="5dp"
    android:layout_height="wrap_content"
    android:background="@color/pending_color_my_schedule" />

</RelativeLayout>

这是整个布局,设置为match_parent。现在视野非常大,占据了整个屏幕。

我无法在不添加详细信息的情况下提交,因此我将添加lorem ipsum,抱歉

Lorem Ipsum只是印刷和排版行业的虚拟文本。自16世纪以来,Lorem Ipsum一直是业界标准的虚拟文本,当时一台未知的打印机采用了类型的厨房,并将其拼凑成一个类型的样本书。它不仅存在了五个世纪,而且还延续了电子排版,基本保持不变。它在20世纪60年代推出了包含Lorem Ipsum段落的Letraset表格,最近还发布了包括Lorem Ipsum版本在内的桌面出版软件Aldus PageMaker。

2 个答案:

答案 0 :(得分:0)

如何将亲戚的layout_height更改为match_parent?如果它无法解决您的问题,请将整个xml放在这里吗?

答案 1 :(得分:0)

我认为您尝试归档的内容与您编码的内容不同,在您的情况下,RecyclerView项目布局应该是这样的

在代码中进行少量更改

1.创建您的recycler_items.xml,如下所示

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_margin="10dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#fafafa">

        <View
            android:id="@+id/status_view"
            android:layout_width="5dp"
            android:layout_height="match_parent"
            android:background="@color/pending_color_my_schedule" />

        <LinearLayout
            android:padding="16dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <!-- Your code here
            Example is given below
            -->
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Hello World!" />
        </LinearLayout>
    </LinearLayout>
</android.support.v7.widget.CardView>

2.更改main_layout.xml,如下所示

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/my_schedule_item_layout"
    android:layout_width="match_parent"
    android:background="#eeeeee"
    android:layout_height="match_parent">
    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:id="@+id/rvHome"
        android:layout_height="match_parent">

    </android.support.v7.widget.RecyclerView>
</RelativeLayout>
  

正确地recyclerview_item.xml height已设置为100dp,这仅用于演示目的。 如果您希望将视图设为wrap_content,可调高度,请将wrap_content 中所有高度属性更改为recyclerview_item.xml(包括父级和子级)


  

添加以下依赖项

implementation 'com.android.support:cardview-v7:27.0.0' //Check your version
implementation 'com.android.support:recyclerview-v7:27.0.0'