133

我在 Listview 中的 Cardview 在 Android L(Nexus 5)中没有显示阴影。圆边也没有正确显示。这是 Listview 的 Adapter View 的代码:

<?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"
xmlns:app="http://schemas.android.com/apk/res/com.example.myapp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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"
    app:cardBackgroundColor="@android:color/white"
    android:foreground="?android:attr/selectableItemBackground"
    app:cardCornerRadius="4dp"
    app:cardElevation="4dp" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingTop="@dimen/activity_vertical_margin" >

        <TextView
            android:id="@+id/tvName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginTop="@dimen/padding_small"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <ImageView
            android:id="@+id/ivPicture"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tvName"
            android:layout_centerHorizontal="true"
            android:scaleType="fitCenter" />

        <TextView
            android:id="@+id/tvDetail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/ivPicture"
            android:layout_centerHorizontal="true"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin" />
    </RelativeLayout>
</android.support.v7.widget.CardView>

和 ListView xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res/com.example.myapp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/app_bg" >

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:cacheColorHint="#00000000"
    android:divider="@android:color/transparent"
    android:drawSelectorOnTop="true"
    android:smoothScrollbar="true" />

<ProgressBar
    android:id="@+id/progressBarMain"
    style="?android:attr/progressBarStyleLarge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:visibility="gone" /></RelativeLayout>

它在具有适当阴影和圆角的 pre-L 设备上运行良好。但不适用于 Android L 设备。你能告诉我这里缺少什么吗?

4

20 回答 20

300

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

只需添加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="2dp"
    card_view:cardUseCompatPadding="true"
    >
...
</android.support.v7.widget.CardView>
于 2015-01-12T11:13:37.563 回答
58

不要忘记绘制阴影必须使用hardwareAccelerated绘图

hardwareAccelerated = true

在此处输入图像描述

hardwareAccelerated = false

硬件加速卡查看

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

于 2016-03-12T20:02:56.810 回答
56

app:cardUseCompatPadding="true"在您的卡片视图中使用 。例如

<android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginRight="@dimen/cardviewMarginRight"
        app:cardBackgroundColor="@color/menudetailsbgcolor"
        app:cardCornerRadius="@dimen/cardCornerRadius"
        app:cardUseCompatPadding="true"
        app:elevation="0dp">
    </android.support.v7.widget.CardView>
于 2015-11-05T06:43:02.200 回答
29

检查manifest 中的hardwareAccelerated 使其为 true ,使其为 false 删除阴影,当 false 阴影出现在 xml 预览中但不在电话中时。

于 2017-03-06T06:59:13.070 回答
27

将此行添加到 CardView....

card_view:cardUseCompatPadding="true" //for enable shadow
card_view:cardElevation="9dp" // this for how much shadow you want to show

尖端

您可以避免 layout_marginTop 和 layout_marginBottom ,因为阴影本身会在其上下占用一些空间。空间量由您将在 card_view:cardElevation="ndp" 中使用的数量定义。

快乐编码(:

于 2018-02-09T18:53:56.917 回答
22

最后,我能够通过向卡片视图添加边距来在棒棒糖设备上获得阴影。这是最终的卡片视图布局:

<?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"
    xmlns:app="http://schemas.android.com/apk/res/com.example.myapp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    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"
    app:cardBackgroundColor="@android:color/white"
    android:foreground="?android:attr/selectableItemBackground"
    android:layout_marginLeft="@dimen/activity_horizontal_margin"
    android:layout_marginRight="@dimen/activity_horizontal_margin"
    android:layout_marginTop="@dimen/padding_small"
    android:layout_marginBottom="@dimen/padding_small"
    app:cardCornerRadius="4dp"
    app:cardElevation="4dp" >

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <TextView
        android:id="@+id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="@dimen/padding_small"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <ImageView
        android:id="@+id/ivPicture"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tvName"
        android:layout_centerHorizontal="true"
        android:scaleType="fitCenter" />

    <TextView
        android:id="@+id/tvDetail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ivPicture"
        android:layout_centerHorizontal="true"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin" />
</RelativeLayout>

于 2015-01-01T11:40:34.887 回答
13

您可以在卡片视图中为阴影添加这行代码

card_view:cardElevation="3dp"

下面你有一个例子

<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"
    app:cardBackgroundColor="@android:color/white"
    android:foreground="?android:attr/selectableItemBackground"
    card_view:cardElevation="3dp"
    card_view:cardCornerRadius="4dp">

希望这可以帮助!

于 2014-12-26T13:17:44.947 回答
13

只需添加以下标签:

app:cardElevation="2dp"
app:cardUseCompatPadding="true"

所以它变成:

<android.support.v7.widget.CardView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:cardBackgroundColor="?colorPrimary"
    app:cardCornerRadius="3dp"
    app:cardElevation="2dp"
    app:cardUseCompatPadding="true"
    app:contentPadding="1dp" />
于 2017-03-08T10:12:15.820 回答
8

您可以尝试通过添加此行

 card_view: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="200dp"
        android:layout_margin="5dp"
        android:orientation="horizontal"
        card_view:cardUseCompatPadding="true"
        card_view:cardCornerRadius="5dp">
 </android.support.v7.widget.CardView
于 2015-10-09T17:36:13.827 回答
7

在我的情况下,阴影没有显示在 Android L 设备上,因为我没有添加边距。问题是 CardView 在 <5 设备上自动创建此边距,所以现在我这样做:

CardView card = new CardView(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
        LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
if (Build.VERSION_CODES.LOLLIPOP == Build.VERSION.SDK_INT) {
    params.setMargins(shadowSize, shadowSize, shadowSize,
            shadowSize);
} else {
    card.setMaxCardElevation(shadowSize);
}
card.setCardElevation(shadowSize);
card.setLayoutParams(params);
于 2014-12-26T10:33:54.190 回答
6

在您的清单文件中添加 android:hardwareAccelerated="true" 如下所示,它适用于我

 <activity
        android:name=".activity.MyCardViewActivity"
        android:hardwareAccelerated="true"></activity>
于 2018-01-30T06:43:44.520 回答
5

如果您正在寻找MaterialCardView阴影(海拔),则在 XML 中添加这行代码,它将显示正确的海拔 -

app:cardElevation="24dp"
app:cardMaxElevation="36dp"
于 2021-03-06T17:53:14.193 回答
4

首先确保在 build.gradle 文件中正确添加和编译以下依赖项

    dependencies {
    ...
    compile 'com.android.support:cardview-v7:21.0.+'

    }

然后尝试以下代码:

     <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="200dp"
            android:layout_margin="5dp"
            android:orientation="horizontal"
            card_view:cardCornerRadius="5dp">
     </android.support.v7.widget.CardView

Android L 中出现问题,因为未添加 layout_margin 属性

于 2015-03-03T07:21:57.817 回答
0

我的 recyclerview 加载速度很慢,所以通过阅读 stackoverflow.com,我将 hardwareAccelerated 更改为“false”,然后设备中没有显示海拔。我变回了真实。这个对我有用。

于 2017-07-13T02:18:43.533 回答
0

卡片视图无法显示阴影,因为您RelativeLayout位于卡片视图的阴影上。要显示阴影,请在您的卡片视图上添加边距。例如:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="4dp"
    android:layout_marginBottom="8dp">

</android.support.v7.widget.CardView>

</RelativeLayout>
于 2019-02-25T06:42:44.953 回答
0

我认为如果你先检查你的清单,如果你写了android:hardwareAccelerated="false" ,你应该让它true有卡的影子,就像这个答案在这里

于 2019-03-21T16:07:23.130 回答
0

检查您没有android:layerType="software"RecyclerView中使用。

更改它以android:layerType="hardware"解决我的问题。

于 2020-04-23T22:55:25.080 回答
0

您还可以通过分配进行测试:

安卓:硬件加速=“假”

在应用程序标签下的清单文件中。

由于有时不支持某些自定义视图,因此将值分配给上述语句“true”或“false”可能是一种解决方案。 有关更多信息,请参阅 android studio 文档

于 2021-05-11T16:52:39.787 回答
0

如果您使用 MaterialCardView 选项是 'app:cardElevation="8dp' "8dp" 是参考尺寸

于 2021-07-27T22:42:40.597 回答
-1

将您的“uses-sdk”属性移动到清单顶部,如下面的答案https://stackoverflow.com/a/27658827/1394139

于 2014-12-27T07:11:38.543 回答