Cardview阴影没有出现在棒棒糖设备中?

时间:2014-12-26 06:46:54

标签: android android-layout android-5.0-lollipop android-cardview

我在我的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>

Refer the attachment

6 个答案:

答案 0 :(得分:91)

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

只需将card_view:cardUseCompatPadding="true"添加到CardView,就会在Lollipop设备上显示阴影。

发生的情况是,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>

答案 1 :(得分:15)

如前所述CardView not showing Shadow in Android L 确保使用hardwareAccelerated = true

绘制视图

hardwareAccelerated = true enter image description here

hardwareAccelerated = false hardwareAccelerated CardView

有关详细信息,请参阅Android Hardware Acceleration

答案 2 :(得分:5)

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

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

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

答案 3 :(得分:1)

某人,

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

<强>机器人:硬件加速= “假”

我尝试了所有建议的东西,但它只对我有效,当我删除该行时,我有这条线的原因是因为我的应用程序使用了许多位图图像而且它们导致应用程序崩溃。

答案 4 :(得分:0)

请尝试将android:hardwareAccelerated =“false”androidManifest文件解决您的问题,因为我也面临同样的问题,并通过在清单中添加1行来解决。

答案 5 :(得分: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">`

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

相关问题