2015-04-01 1876 views
28

我正在使用Cardview作为我正在写入的自定义视图的根。我使用v7支持库。我的XML看起来像这样:Cardview - 卡周围的白色边框

<?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"

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

setCardElevation(0); 
setMaxCardElevation(0); 

但白色边框依然存在。我不知道如何摆脱它。如果任何人有任何意见,为什么会发生这种情况或建议如何删除白色边框,将不胜感激。非常感谢。

+1

可以共享屏幕抓取容易理解 – Fahim 2015-04-01 14:01:13

+0

你还有这个“白边”,如果你删除'机器人:layout_marginRight =“6DP “'? – Rami 2015-04-01 14:06:16

+0

@Rami - 是的,它仍然存在 – TheMethod 2015-04-01 14:16:47

回答

76

我知道这是一个有点晚,但有类似的问题的人:

我有同样的问题:一个白色边框已于棒棒糖预设备中。

我解决了它在你的XML上设置cardPreventCornerOverlapfalse

像这样:

<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> 

希望这有助于!

+2

这是应用程序:cardUseCompatPadding =“真”,因为我来自的xmlns延伸:应用= “http://schemas.android.com/apk/res-auto”。小心家伙!时间消费! xd – ArturoNaredo 2015-09-02 18:53:32

+2

谢谢!有用。但有一个问题。添加此标签后,它将消除角落半径。我该如何解决这个问题? – 2015-09-14 16:19:13

+0

@CanUludağ,我不认为这将有可能使用支持库。在棒棒糖之前,圆角过于昂贵,所以CardView通过添加填充来防止剪裁边界。使用'cardPreventCornerOverlap'去除它也会去掉圆角。我相信要解决它,你将不得不使用自定义背景图像。 – 2015-09-15 18:47:44

3

支持CardView不支持剪裁内容,因为它在较旧的设备上很昂贵。可以使用Canvas.saveLayer/restoreLayer和PorterDuff模式剪辑内容。这是Carbon如何使用正确的内容剪辑实现圆角。查看图片:在XML card_view 示例代码

enter image description here

-4

使用cardBackgroundColor = “颜色”:

<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"/> 
+0

这并没有解决这个问题。 – 2016-05-25 05:35:57

2

我可能会在比赛中迟到,但我有同样的问题。只是想分享一个简单的解决方案!

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

Before

的解决办法是保证金从自定义视图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" /> 

变更后,干净多了:):

enter image description here

相关问题