2017-01-09 179 views
49

我正在使用RecyclerView和GridLayoutManager,並且每個項目都是CardView。CardView背景顏色始終爲白色

不幸的是,這裏的CardView似乎沒有改變它的背景顏色。我試圖在佈局和編程方面,但我已經試過似乎沒有工作。

我一直在掙扎了很長一段時間。我很感激有人能幫我解決這個問題。

+1

card_view:cardBackgroundColor =「@安卓:顏色/白色「檢查與此。 –

+0

需要幫助的代碼 – Manza

+0

這是非常長的代碼,因此我沒有發佈它。讓我發佈一些相關的代碼。 – Ishaan

回答

118

如果要更改卡的背景顏色,使用:

app:cardBackgroundColor="@somecolor" 

這樣的:

<android.support.v7.widget.CardView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:cardBackgroundColor="@color/white"> 

</android.support.v7.widget.CardView> 

編輯: 正如指出的@imposible,你需要包括

xmlns:app="schemas.android.com/apk/res-auto" 

在您的根XML標記中爲了使這個片段功能

+2

謝謝你的回答,非常感謝。 – Ishaan

+1

它適用於我,當我用它這樣---> card_view:cardBackgroundColor =「@ color/white」 –

+1

在那,有xmlns:app =「http://schemas.android.com/apk/res- auto「包含在你的佈局文件中。 – impossible

18

你可以做到這一點無論是在XML或以編程方式:

在XML:

card_view:cardBackgroundColor="@android:color/red" 

編程:

cardView.setCardBackgroundColor(Color.RED); 
+0

謝謝你的回答,我解決了它。我以編程方式將顏色更改爲某個我無法識別的地方。 – Ishaan