android - Cardview 阴影没有出现在 Lollipop 设备中?

标签 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

最佳答案

又翻了一遍文档,终于找到了解决办法。

只需将 card_view:cardUseCompatPadding="true" 添加到您的 CardView 中,Lollipop 设备上就会出现阴影。

发生的情况是,CardView 中的内容区域在 Lollipop 前和 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>

关于android - Cardview 阴影没有出现在 Lollipop 设备中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27653781/

相关文章:

android - Android Lollipop 中应用程序外部配置文件和内部配置文件之间的数据传输

android - parcelable Kotlin 的 parcelable objects 列表

android:background 在 style.xml 中不起作用

android - LinearLayout 作为自定义按钮,OnClickListener 从未调用

android - 在 API 11 之前捕获布局调整大小

Android layout_width & layout_height,它是如何工作的?

android - 膨胀类 RecyclerView 时出错

android - 为什么在设置所有者设备应用程序时无法创建受限配置文件?

Android Espresso Ui 测试验证 ActionPage 的标签文本

android - 类声明错误, "testbuild"无法解析为类型