Cardview - 卡片周围的白色边框

Cardview - white border around card

我正在使用 cardview 作为我正在编写的自定义视图的根。我使用 v7 支持库。我的 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:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginRight="6dp"
        card_view:cardElevation="0dp">

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

        <!-- some other views -->
    </LinearLayout>
</android.support.v7.widget.CardView>

我的问题是卡片视图周围出现白色边框。它看起来像是在指示高度,因为它在右侧较厚。我试过像这样调整 XML 中的 cardElevationMaxCardElevationcard_view:cardElevation="0dp"

在扩展 CardView 并使用此布局的自定义视图中的代码中:

setCardElevation(0);
setMaxCardElevation(0);

但是白色边框仍然存在。我不确定如何摆脱它。如果有人对发生这种情况的原因有任何意见或对我如何删除白色边框有任何建议,我们将不胜感激。非常感谢。

我知道有点晚了,但对于遇到类似问题的人来说:

我遇到了同样的问题:棒棒糖之前的设备上显示了白色边框。

我解决了将 XML 上的 cardPreventCornerOverlap 设置为 false 的问题。

像这样:

<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="wrap_content"
    android:layout_height="match_parent"
    android:layout_marginRight="6dp"
    card_view:cardPreventCornerOverlap="false">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <!-- some other views -->
    </LinearLayout>
</android.support.v7.widget.CardView>

希望对您有所帮助!

支持 CardView 不支持内容剪辑,因为它在旧设备上很昂贵。可以使用 Canvas.saveLayer/restoreLayer 和 PorterDuff 模式剪辑内容。这就是 Carbon 如何通过正确的内容剪辑实现圆角。见图:

在 xml card_view 中使用 cardBackgroundColor="color" 示例代码:

<android.support.v7.widget.CardView
        android:id="@+id/card_view_khaterat"
        android:layout_width="250dp"
        android:layout_height="100dp"
        android:layout_gravity="center_horizontal"
        app:cardBackgroundColor="#f56f6c"/>

我可能迟到了,但我遇到了同样的问题。只是想分享一个简单的解决方案!

我的自定义视图扩展了一个 CardView 并且我在 XML 中对根元素(即 CardView)应用了边距,就像在原始问题中一样。这最终给了我这样的额外白色边框:

解决方案是将边距从自定义视图的根 XML 移动到自定义视图的声明(检查代码段中的注释)

代码片段:

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cvSettings"
    style="@style/DefaultCardViewStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/default_margin" <!-- Remove this -->
    app:cardElevation="@dimen/default_card_elevation">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        .
        .
        .
</android.support.v7.widget.CardView>

最后只是移过边距到我的自定义视图声明,它去掉了多余的白色边框:

<com.example.android.custom.MyCustomView
        android:id="@+id/custom_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/default_margin" <!-- Move it here-->
        cv:pt_widgetColor="@color/colorAccent" />

改完之后,干净多了:) :