Cardview - 卡片周围的白色边框。

34

我正在使用一个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中调整cardElevationMaxCardElevation,像这样:

card_view:cardElevation="0dp"

以及在使用此布局的自定义视图中的代码中。

setCardElevation(0);
setMaxCardElevation(0);

但是白色边框仍然存在,我不确定如何去掉它。如果有人知道为什么会出现这种情况或者有任何建议可以帮助我去掉白色边框,将不胜感激。非常感谢。


1
你能分享屏幕截图以便更容易理解吗? - Fahim
如果您删除 android:layout_marginRight="6dp",您是否仍然有这个“白色边框”? - Rami
@Rami - 是的,它仍然在那里。 - TheMethod
好的,如果您发布一个视图的屏幕截图将会很有帮助。 - Rami
我也在苦恼这个问题,该怎么解决呢? - harshal
我的问题是在我创建的CardView子类中从xml膨胀CardView。我将其更改为扩展RelativeLayout而不是CardView,并且它可以工作。更多细节:https://dev59.com/XpTfa4cB1Zd3GeqPU76-?rq=1 - Justin
4个回答

87

我知道可能有点晚了,但是如果有人遇到类似的问题:

我也遇到了同样的问题:在Android 5.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>

3
由于我扩展自xmlns:app =“http://schemas.android.com/apk/res-auto”,因此它是app:cardUseCompatPadding =“true”。大家要小心!这需要花费时间!XD - ArturoNaredo
2
谢谢!它有效了。但是有一个问题。添加这个标签后,它会取消圆角半径。我该怎么解决? - Can Uludağ
2
cardPreventCornerOverlap="false" 和 contentPadding="0dp" - ademar111190
4
对我来说,应该是app:cardPreventCornerOverlap="false"而不是card_view:cardPreventCornerOverlap="false"。我进行了更改并且它起作用了。 - Danilo Prado
我在父布局中将其设置为xmlns:card_view="http://schemas.android.com/tools",这导致了一些问题。请注意xmlns:card_view="http://schemas.android.com/apk/res-auto" - Arjun Sunil Kumar
显示剩余3条评论

3

支持卡片视图不支持内容裁剪,因为在旧设备上成本较高。可以使用Canvas.saveLayer/restoreLayer和PorterDuff模式来裁剪内容。这就是Carbon如何实现正确内容裁剪的圆角。请参见以下图片:

enter image description here


3
我可能在晚了一些,但我遇到了同样的问题。只想分享一个简单的解决方案!
我的自定义视图扩展了 CardView,并且我在 XML 中的根元素(即 CardView)上应用了一个 margin,就像原来的问题一样。这最终给我带来了额外的白色边框,就像这样: Before 解决方案是将自定义视图 XML 的 margin 从根元素移动到自定义视图的声明中(请查看片段中的注释)
代码片段:
<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" />

变更后,非常清晰 :) :

输入图片描述


-4

在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"/>

1
这根本没有解决问题。 - Luke Allison

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接