Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawable as Background for CardView

the CardView ( android.support.v7.cardview ) stays white even though I set a backround drawable via android:backround - The documentation gives me the feeling that it should work. No Idea what I am doing wrong here.

like image 272
ligi Avatar asked Dec 04 '14 16:12

ligi


People also ask

How do you set drawable background in CardView?

Make Cardview will host one viewgroup for eg relative layout in this case and then simply set any background to relative layout. Show activity on this post. I got this working by adding a linearlayout in the cardview and then setting cardPreventCornerOverlap to false in the cardview. Show activity on this post.

How do you put a background color on CardView?

card_view:cardBackgroundColor="@android:color/white" check with this.

How do I add a gradient background in CardView?

try setting cardview height to wrap content and set the text view height to 44dp. You can also add a LinearLayout to the cardView, set the height and width to match parent, set the gradient background, then add the textview inside the LinearLayout.


1 Answers

I know this is an old question, but I have a simple solution - just make the first child of your CardView an ImageView and specify the scale type to fitXY. You can get rid of the extra CardView padding by setting cardElevation and cardMaxElevation to 0dp:

<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     android:layout_width="match_parent"     android:layout_height="200dp"     app:cardCornerRadius="4dp"     app:cardElevation="0dp"     app:cardMaxElevation="0dp">      <ImageView         android:src="@drawable/your_background"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:scaleType="fitXY"/>      <... your layout      .../>   </android.support.v7.widget.CardView> 
like image 184
Phil Avatar answered Oct 04 '22 15:10

Phil