在Android 5.0上的AppCompat CardView上设置XML高程

The*_*ter 92 android android-appcompat android-cardview android-elevation

根据我的理解,在预览阶段的早期,似乎没有办法只在CardView没有Java的黑客攻击的情况下在XML中设置高程.既然官方发布已经发布,有没有办法在XML中执行此操作而无需编写Java代码来设置高程?

我试过card_view:cardElevation没有效果.我曾经想过当我使用5.0的模拟器时一切都很好.但是现在我在我的实际设备上使用正式版本,我的所有内容都CardView消失了

Pre Lollipop,效果很好.

这是我的完整xml

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="horizontal"
    android:layout_width="match_parent"
    android:id="@+id/cv1"
    card_view:cardElevation="4dp"
    android:layout_margin="6dp"
    card_view:cardCornerRadius="3dp"
    android:layout_height="match_parent">
Run Code Online (Sandbox Code Playgroud)

Gaë*_* M. 310

它看起来像边距/填充问题,尝试将cardUseCompatPadding属性设置为true.例如:

<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_height="match_parent"
    android:layout_margin="6dp"
    card_view:cardUseCompatPadding="true"
    card_view:cardElevation="4dp"
    card_view:cardCornerRadius="3dp">
Run Code Online (Sandbox Code Playgroud)

Android doc的解释:

CardView添加额外的填充以在L之前在平台上绘制阴影.

这可能导致卡片在L和L之间具有不同的大小.如果需要将CardView与其他视图对齐,则可能需要api版本特定的维度资源来考虑更改.作为替代方案,您可以将cardUseCompatPadding标志设置为true,并且CardView将在平台L和之后添加相同的填充值.

由于将cardUseCompatPadding标志设置为true会在UI中添加不必要的间隙,因此默认值为false.


mat*_*ani 13

如果你有这条线

android:hardwareAccelerated="false"
Run Code Online (Sandbox Code Playgroud)

在清单应用程序标记中,您的阴影未显示.尝试删除此行

或使用

android:hardwareAccelerated="true"
Run Code Online (Sandbox Code Playgroud)

这对我有用!我希望它也适合你:)


Gab*_*tti 12

您必须使用cardElevation属性

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    card_view:cardElevation="4dp" />
Run Code Online (Sandbox Code Playgroud)