卡片视图在Lollipop设备上没有阴影显示?

32

我正在使用卡片视图(CardView)来开发我的安卓应用。然而,阴影效果没有显示出来。以下是xml布局代码:

默认选项菜单(optionsmenu)的阴影效果也没有显示。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ECEDF0"
    android:orientation="vertical" >

    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:clipChildren="false"
        card_view:cardBackgroundColor="@color/white"
        card_view:cardCornerRadius="4dp"
        card_view:cardElevation="14dp"
        card_view:cardUseCompatPadding="true" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="20dp"
            android:text="Google Play" />
    </android.support.v7.widget.CardView>

</LinearLayout>

Refer the attachment


我认为LinerLayout的背景和阴影的颜色很相似。尝试更改LinerLayout的颜色,然后检查它是否可见。 - Pankaj Kumar
改了颜色但没有成功 @PankajKumar - Dinesh T A
有些牵强,但我见过这种情况发生,尝试将 CardView 下面的 xmlns:card_view="http://schemas.android.com/apk/res-auto" 删除,这样你只需要在 LinearLayout 中保留一个。 - zoltish
尝试过了,但阴影不可见。 - Dinesh T A
6个回答

91

在再次查看文档后,我终于找到了解决方法。

只需在你的CardView中添加card_view:cardUseCompatPadding="true",就可以使阴影在Lollipop设备上显示。

问题在于,CardView中的内容区域在不同的Lollipop版本和之前版本中大小不同。因此,在Lollipop设备上,阴影实际上被卡片覆盖,因此不可见。通过添加此属性,内容区域在所有设备上保持相同,阴影变得可见。

我的XML代码如下:

<android.support.v7.widget.CardView
    android:id="@+id/media_card_view"
    android:layout_width="match_parent"
    android:layout_height="130dp"
    card_view:cardBackgroundColor="@android:color/white"
    card_view:cardElevation="2sp"
    card_view:cardUseCompatPadding="true"
    >
...
</android.support.v7.widget.CardView>

你能帮我解决这个问题吗?https://stackoverflow.com/questions/50066346/cardview-gridlayout-with-recyclerview-is-not-fit-for-all-screen-sizes-in-andro - Nikson

16

5

对于Lollipop及更高版本,您应该为卡片添加一些边距:

<android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            />

因为阴影位于实际视图之外。


你救了我的一天。在Lollipop上,cardElevationcardUseCompatPadding没有边距不起作用。 - Zeeshan Ghazanfar

1

对于某些人来说,还有一件事情需要注意,如果在清单文件中添加了以下这行代码,阴影将不会显示:

android:hardwareAccelerated="false"

我尝试了所有建议的方法,但只有当我删除了这行代码时才起作用。我加入这行代码的原因是我的应用程序使用了许多位图图像,它们导致应用程序崩溃。


1
谢谢@adnan,我进行了很多搜索,但无法在cardview上进行高程或阴影处理,只需从manifest.xml中删除android:hardwareAccelerated="false",现在它看起来就像我需要的那样。 - Zafar Imam

0

嘿,朋友们,我找到了上述问题的解决方案。只需在您的xml中添加此代码即可。

<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:layout_width="match_parent"
    android:layout_margin="8dp"
    android:id="@+id/griditem"
    android:layout_height="match_parent"
    card_view:cardUseCompatPadding="true"
    card_view:cardElevation="4dp"
    card_view:cardCornerRadius="3dp">`

希望对你有所帮助...


0
请尝试在AndroidManifest文件中添加android:hardwareAccelerated="false",这可能会解决您的问题,因为我也遇到了同样的问题,并通过在清单中添加一行代码解决了它。

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