79

我尝试使用CardView它,它在 5.0 以下运行良好,但在 Lollipop 上看起来很奇怪。

在此处输入图像描述

在此处输入图像描述

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">

<android.support.v7.widget.CardView android:layout_width="match_parent"
    android:layout_height="200dp">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="card1"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView android:layout_width="match_parent"
    android:layout_height="200dp">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="card2"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</android.support.v7.widget.CardView>
</LinearLayout>

我在使用时遇到了同样的问题RecyclerView,如果它在 Lollipop 上运行,我是否必须添加一些东西?

4

4 回答 4

205

将此设置在CardView

app:cardUseCompatPadding="true"

从文档:

在 API v21+ 中添加填充以与以前的版本具有相同的测量值。

于 2014-12-09T01:30:55.340 回答
29

在您的卡片视图中使用下面的这两个标签:

app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true"
于 2015-05-21T21:11:33.463 回答
13

第一个图像是卡片视图的预期行为。当卡片有高度时,阴影落在底层。在棒棒糖之前的设备中,通过添加填充来实现高程。所以棒棒糖前的设备将在卡片视图周围有一个填充。

在 L 之前,CardView 为其内容添加填充并在该区域绘制阴影。此填充量等于 maxCardElevation + (1 - cos45) *cornerRadius 在侧面和 maxCardElevation * 1.5 + (1 - cos45) *cornerRadius 在顶部和底部。

于 2014-11-20T09:19:54.250 回答
5

您必须添加app:cardUseCompatPadding="true"到您的Cardview. 但只是添加它可能会给你一个错误。为避免该错误,您还必须添加xmlns:app="http://schemas.android.com/apk/res-auto"到您的CardView.

例如,

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    app:cardUseCompatPadding="true">

    // Other views here

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

有些人会添加card_view:cardUseCompatPadding="true"andxmlns:card_view="http://schemas.android.com/apk/res-auto"而不是上面提到的那些。两种方式都是正确的。

如果您想了解更多关于XML(Android) 中的应用程序的信息,请查看此答案

尽管以前的答案可以解决问题,但他们没有解释每个属性的作用。所以为了更有助于回答寻求者,

cardPreventCornerOverlap属性在 v20 及之前的版本中为 CardView 添加填充,以防止卡片内容和圆角之间的交叉。

cardUseCompatPadding属性在 API v21+ 中添加了填充,以便与以前的版本具有相同的测量值。

于 2016-08-04T13:34:43.983 回答