回收器视图项中视图之间的空白

时间:2015-10-24 16:55:12

标签: android padding android-recyclerview android-cardview

我在回收站视图中为每个项目使用卡片视图,但是我在图片下方和上方获得了空白区域,我不知道如何,我需要摆脱它。这是每个项目的xml布局。< / p>

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cv_custom"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginEnd="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginStart="10dp"
    android:clickable="true"
    android:elevation="2dp"
    android:foreground="?android:attr/selectableItemBackground"
    card_view:cardCornerRadius="1dp"
    card_view:cardElevation="2dp"
    card_view:cardUseCompatPadding="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/image_path"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:contentDescription="@string/event_image" />

        <TextView
            android:id="@+id/custom_event_name"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:ellipsize="marquee"
            android:gravity="start|top"
            android:maxLines="1"
            android:padding="5dp"
            android:textColor="@color/parent"
            android:textSize="20sp"
            android:textStyle="bold" />

    </LinearLayout>
</android.support.v7.widget.CardView>

这是我目前使用xml提供的内容。一个项目我有一个图像,一个是没有。将回收者视图的背景变为红色,以便间隙更明显。

Sample

2 个答案:

答案 0 :(得分:3)

ImageView内您需要提供比例

android:scaleType="centerCrop"

您看到空白区域的原因是因为图像和容器的纵横比不同。

想象一下,您正试图将宽矩形放入正方形中。你可以装入它,但顶部和底部将是空的&#34;或者在这种情况下是白色。

如果进行中央裁切,则裁剪图像。这会将矩形切割成方形,因此非常适合!

答案 1 :(得分:0)

我过去通过在视图下放置空格来解决这个问题,但我不喜欢视图列表末尾的所有额外空间。但我意识到,只要把一半的空间放在上面,一半放在下面(在列表项目上)。然后,当它们组合在一起时,它在项目之间的空间是两倍,而填充列表顶部和底部的正常空间。