32

我在我的 android 应用程序中使用 cardview。然而阴影并没有出现。这是xml布局

默认的选项菜单阴影也没有显示。

<?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>

参考附件

4

6 回答 6

91

再次浏览文档后,我终于找到了解决方案。

只需添加card_view:cardUseCompatPadding="true"到您的CardView和阴影将出现在棒棒糖设备上。

发生的情况是,内容区域在CardView预棒棒糖和棒棒糖设备上采用不同的大小。因此,在棒棒糖设备中,阴影实际上被卡覆盖,因此不可见。通过添加此属性,内容区域在所有设备上保持相同,并且阴影变得可见。

我的 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>
于 2015-01-12T11:04:21.883 回答
16

如前所述,CardView 在 Android L 中未显示阴影, 请确保您正在使用绘制视图hardwareAccelerated = true

hardwareAccelerated = true 在此处输入图像描述

hardwareAccelerated = false 硬件加速卡查看

有关详细信息,请参阅Android 硬件加速

于 2016-03-12T20:11:31.427 回答
5

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

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

由于阴影位于实际视图之外

于 2016-08-24T12:55:48.353 回答
1

给某人 ,

您应该注意的另一件事是,如果清单中有此行,阴影将不会显示:

安卓:硬件加速=“假”

我尝试了所有建议的东西,但它仅在我删除该行时才对我有用,我有该行的原因是因为我的应用程序与许多位图图像一起使用并且它们导致应用程序崩溃。

于 2017-04-03T05:21:53.533 回答
0

请尝试输入 android:hardwareAccelerated="false" androidManifest 文件可能会解决您的问题,因为我也遇到了同样的问题并通过仅在清单中添加 1 行来解决。

于 2017-07-25T07:36:57.323 回答
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">`

我希望它对你有帮助...

于 2018-05-13T18:10:11.850 回答