android:layout_height 不适用于 CardView

Ume*_*ooq 5 android android-cardview

我正在使用 GridLayoutManager 在 RecyclerView 中使用 CardViews。我面临的问题是,我在 xml 或 java 中为卡片视图指定的高度,它不会由 CardView 强制执行。看起来CardView 的子视图累积高度变成了 CardView 的高度。这种行为考虑到它是在其子视图上强制执行的父布局参数的事实。

我错过了什么?难道我做错了什么?

<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/cardView"
    android:layout_width="@dimen/card_view_width"
    android:layout_height="0dp"
    android:layout_marginBottom="6dp"
    android:elevation="10dp"
    android:orientation="horizontal"
    card_view:cardCornerRadius="2dp"

    >

<ImageView
 android:layout_width="@dimen/card_view_width"
    android:layout_height="10dp"
></ImageView>

</android.support.v7.widget.CardView>
Run Code Online (Sandbox Code Playgroud)

在这种情况下,卡片视图的高度为 0dp,但仍然是布局设计器预览,当应用程序在设备上运行时,卡片的大小为 10dp。

此致

Ale*_*x K 1

根据您告诉程序的内容,您预计会发生什么?

你告诉CardVeiw高度为0dp: android:layout_height="0dp"

然后你放置一个ImageView内部,并告诉它的高度为10dp

因此,当然,它CardView会使自己更高,以使其ImageView能够正确贴合。

如果您希望内部元素与 的高度相匹配CardView,请将 的 设为您想要的任何值,layout_height例如:CardView20dp

android:layout_height="20dp"
Run Code Online (Sandbox Code Playgroud)

然后有内部元素match_parent

android:layout_height="match_parent"
Run Code Online (Sandbox Code Playgroud)