【问题标题】:Android CardView inside RecyclerView - cardElevation cause unwanted margin & bottom shadowRecyclerView 中的 Android CardView - cardElevation 导致不需要的边距和底部阴影
【发布时间】:2017-07-25 05:27:10
【问题描述】:

在 Android 中,我使用的是RecyclerView。对于 RecyclerView 的 item,我使用 CardView 作为 item 的布局,xml 布局如下:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="0dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="0dp"
    app:cardElevation="2dp"
    app:cardCornerRadius="0dp"
    app:cardPreventCornerOverlap="false"

    android:foreground="?attr/selectableItemBackground"
    android:clickable="true" >

    <LinearLayout
        android:minHeight="48dp"
        android:layout_marginTop="0dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginBottom="0dp"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    .......

    </LinearLayout>
</CardView>

我正在使用来自 android.support.v7.widget.CardView 的 CardView。

我设置了cardElevation="2dp", layout_marginBottom="0dp", layout_marginTop="0dp"。设置 cardElevation 会在 CardView 的左、右和底部产生阴影。然后通过设置 layout_marginBottom & layout_marginTop=0dp,如果 item 不是列表中的最后一个 item,它的底部阴影将 disappear,因为它被列表中的下一个 item (CardView) 覆盖。

以下是预期的 UI 行为。整个 Watchlist 看起来像单个 CardView,但实际上它是一个 RecyclerView,每个项目由 CardView 组成,只是底部阴影被它下面的下一个项目覆盖。这在 Android API 25 (Android 7.1.2) 中运行良好:

但在 Android API 16 (Android 4.1.2) 上,结果并不如预期。底部阴影(边框)仍然可见,看起来每个项目之间都添加了额外的边距。

知道如何在 Android 4.1.2 中解决这个问题吗?

【问题讨论】:

  • Elevation 属性仅适用于超过 21 的 Android API。
    查看this 链接以获得在 API 21 以下实现所需效果的想法:
  • OP 没有问为什么Elevation 不起作用。 (她正在使用支持库中的app:cardElevation。因此,在 Android 4 中看到提升效果是有意义的)她在问为什么 Android 6 中的行之间没有 no 空间,但行之间有空间在 Android 4 中。

标签: android android-recyclerview android-cardview android-elevation


【解决方案1】:

我的原始猜测是,您使用的是支持库中的CardView。不同版本的Android有不同的卡片视图实现。

我相信在 Android L 之前,该库添加填充是为了为阴影绘制保留空间。

详情请查看CardView inside RecyclerView has extra margins

【讨论】:

    猜你喜欢
    • 2014-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-26
    • 1970-01-01
    相关资源
    最近更新 更多