2017-03-29 126 views
1

我嘗試添加自定義背景在我CardView.This是我的源CardView自定義背景

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/item_container" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:background="#ff00" 
android:orientation="vertical"> 


<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    card_view:cardCornerRadius="12dp" 
    card_view:cardElevation="12dp" 
    card_view:cardPreventCornerOverlap="false" 
    card_view:contentPadding="0dp" 
    android:layout_margin="16dp" 
    > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="80dp" 
     android:orientation="vertical"> 
     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="40dp" 
      android:background="#cccccc"> 
     </RelativeLayout> 
    </LinearLayout> 
</android.support.v7.widget.CardView> 

And this is a result

正如你可以看到成Cardview我有LinearLayout中和RelativieLayout.RelativeLayout的背景色爲# cccccc,但是cornerradius和elevetion不能用自定義背景顏色(請看我的圖片) 我怎麼能解決我的問題?

回答

1

線性佈局CardView重疊cardview的頂部邊角由於固定給線性佈局高度。當給cardview設置高度並製作線性佈局match_parent,並在cardview中給出contentPadding時,可以看到角落。

這裏是代碼和截圖。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/item_container" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:background="#ff00" 
android:orientation="vertical"> 


<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="80dp" 
    card_view:cardCornerRadius="12dp" 
    card_view:cardElevation="12dp" 
    card_view:cardPreventCornerOverlap="true" 
    card_view:contentPadding="2dp" 
    android:layout_margin="16dp"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 
     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="40dp" 
      android:background="#cccccc"> 
     </RelativeLayout> 
    </LinearLayout> 
</android.support.v7.widget.CardView> 
</LinearLayout> 

截圖

Image

+0

它是一個可以改變背景顏色沒有利潤?我的意思是保證金是左右? @Vivek Mishra – BekaKK

+0

我認爲使用像這樣的圖像作爲你的cardview背景會更好。 –

+0

ccccc顏色的實例? @Vivek Mishra – BekaKK