Android 4.4如何移除CardView的阴影?

3
我将尝试为 Cardview 移除阴影效果。
我在 Android v4.4.2 上使用了 android support v7 的 Cardview。

我的布局如下所示:

<android.support.v7.widget.CardView
    android:id="@+id/cardContainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@null"
    card_view:cardBackgroundColor="@color/transparent"
    card_view:cardCornerRadius="0dp"
    card_view:cardElevation="0dp"
    card_view:cardPreventCornerOverlap="false"
    card_view:contentPadding="0dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <include layout="@layout/include_profile_header" />

        <include layout="@layout/include_profile_footer" />

    </RelativeLayout>

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

正如您所见,我使用了:
  card_view:cardElevation="0dp" 

目前为止尝试过的方法:

android:background="@null"  

但是你仍然可以在我附上的图片中看到顶部显示的阴影。 CardView Shadow

尝试移除cardBackgroundColor @motis10 - Jhaman Das
android:cardBackgroundColor不存在,而card_view:cardBackgroundColor也没有。 - motis10
card_view:cardBackgroundColor="@color/transparent" 请删除此内容。 - Jhaman Das
但是它会添加一个白色背景到我的布局中。正如您在图像中所看到的,我希望它是透明的。 - motis10
3个回答

5
只需将以下代码添加到“cardview”标记即可:
app:cardElevation="0dp"

不要忘记添加

xmlns:app="http://schemas.android.com/apk/res-auto"

更新

同时,尝试将以下代码放入卡片视图中:

app:cardElevation="0dp"
app:cardMaxElevation="0dp"
app:cardCornerRadius="1dp"
app:cardUseCompatPadding="true"
app:cardPreventCornerOverlap="false"
android:background="@null"

app:cardCornerRadius的值应该大于0(>0),否则会抛出异常。

我正在使用API 28的支持库。

implementation 'com.android.support:cardview-v7:28.0.0'


2
您无法在4.4中去除CardView的阴影。低于5.0的高度不会产生任何效果,因此在4.4中,整个CardView只是一个带有阴影的图像。
要创建带有阴影的Card,请使用card_view:cardElevation属性。CardView在Android 5.0(API级别21)及以上版本上使用真实高度和动态阴影,并在早期版本上回退到编程实现的阴影。有关更多信息,请参见“保持兼容性”。

http://developer.android.com/training/material/lists-cards.html#CardView


那就意味着我需要将布局的 CardView 更改为另一个 ViewGroup,并手动添加阴影,是这样吗? - motis10
但是,这听起来很奇怪。你说在5.0之前的CardView没有使用cardElevation、maxcardElevation和cardPreventCornerOverlap。 - motis10
@motis10,我不明白你在说什么。 - tyczj
海拔在5.0之前不存在,那怎么能使用它呢? - tyczj
我在一些地方使用了它,但在一个地方它应该没有阴影,我可能会使用帧布局。谢谢。 - motis10
显示剩余3条评论

0

我刚刚通过使用相对布局来解决了这个问题。只需将CardView的值更改为相对布局,您就可以基本上获得相同的结果,但没有阴影。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接