2015-04-01 401 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