如何删除 cardview 中不必要的顶部填充?

How can i remove unnecessary top padding in cardview?

我设法在我的应用程序中实现了卡片视图,但卡片视图在顶部显示了不必要的填充。

我想要实现的是获得这样的 header 图像 :

这是我的卡片视图布局文件:

<android.support.v7.widget.CardView
android:id="@+id/programCardview"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
card_view:cardUseCompatPadding="true"
xmlns:card_view="http://schemas.android.com/apk/res-auto">

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

    <ImageView
        android:id="@+id/programHeader"
        android:layout_width="match_parent"
        android:layout_height="160dp"
        android:src="@drawable/createdprogramviewcard"/>

    <RelativeLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:paddingBottom="16dp"
        android:paddingLeft="16dp"
        android:paddingRight="16dp">

        <TextView
            android:id="@+id/programTitle"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:text="Programme d'endurance"
            android:textSize="20sp"
            android:textAlignment="center"/>

        <TextView
            android:id="@+id/objectif"
            android:layout_below="@+id/programTitle"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="Objectif : " />

        <TextView
            android:id="@+id/programObjectif"
            android:layout_below="@+id/programTitle"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="6 séances"
            android:layout_toRightOf="@id/objectif"/>

        <TextView
            android:id="@+id/programWorkouts"
            android:layout_below="@+id/programTitle"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:textAlignment="textEnd"
            android:layout_alignParentRight="true"
            android:text="6 séances" />


    </RelativeLayout>

</LinearLayout>

这是 RecyclerView 的代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" 

android:layout_height="match_parent" android:padding="16dp">

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerViewPrograms"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

我设法更改了 cardUseCompatPadding 属性,但这并不影响 cardview 的内部形式,它只是将它们分开。

提前致谢。

我通过将 imageView 的高度更改为 130dp 找到了解决问题的方法。显然,因为我将图像设置为 match_parent,所以我必须找到适合 cardview 内图像的确切高度,而不需要给它一些额外的填充。

这种情况有两个原因,第一个是CardView的高度属性在match_parent 或比组件占用更大的尺寸,同时布局的 gravity 属性 在 center.

要修复它,只需将 LinearLayout 的重力设置为中心 |填充或只是填充。