Android 为什么棒棒糖上的卡片视图之间没有空间?

Android 为什么棒棒糖上的卡片视图之间没有空间?,android,android-cardview,Android,Android Cardview,我尝试使用cardwiew,它在5.0以下运行良好,但在棒棒糖上看起来很奇怪 <?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:orientati

我尝试使用
cardwiew
,它在5.0以下运行良好,但在棒棒糖上看起来很奇怪

<?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
时,我遇到了同样的问题,如果它在棒棒糖上运行,我是否应该添加一些内容?

第一张图像是卡片视图的预期行为。当卡片具有标高时,阴影会落在最底层上。在pre-lollipop设备中,提升是通过添加填充物来实现的。因此,预棒棒糖设备将在卡片视图周围有一个填充

在L之前,CardView将填充添加到其内容中,并为其绘制阴影 那个地区。此填充量等于maxCardElevation+(1- cos45)*侧面拐角半径和MaxCard标高*1.5+(1- cos45)*顶部和底部的拐角半径


卡片视图上设置此项:

app:cardUseCompatPadding="true"
从文件:

在API v21+中添加填充,使测量值与 以前的版本


在您的cardview内部使用以下两个标签:

app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true"

您必须将
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>

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

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


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

在卡片之间添加
LinearLayout
,我们在编辑器中看到的卡片视图的大小不是我们在棒棒糖下面可以使用的实际大小,其中有一部分用于添加填充,在棒棒糖上,布局中的大小与实际大小相同,看起来填充看起来更好,但是我应该怎么做才能把它们分开呢?你在编辑器中看到的就是你在现实中得到的。使所有屏幕上的卡片之间具有相同的边距。如果API水平为21或更高,则设置边距。此答案正确。除此之外,如果要使UI一致,不为值-21添加单独的边距,可以将compat padding设置为true,以便它也在L中添加相同的边距@billgates在添加边距后,cardview在lollpop和pre lollpop上看起来都不错,谢谢你的回答!是否可以删除棒棒糖前设备上的填充?我使用了“app”名称空间。“card_view”是否仍在使用?@wsgeorge这是Android文档中的内容,但我已将其编辑为更友好的应用程序名称空间。看起来更好,但如何删除分隔线?@nonews您可以尝试
Android:divider=“@null”