Drawable as Background for CardView

后端 未结 11 960
傲寒
傲寒 2020-12-08 02:29

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

相关标签:
11条回答
  • 2020-12-08 02:41

    The command I used was:

    cardView.setBackgroundResource(R.drawable.card_view_bg);
    

    where card_view_bg is a custom XML resource file.

    If you have some layouts inside the card view and these layouts are overlapping with the margins of the card view then you might need a separate custom background resource file for the layouts like the one I used for the card view background itself.

    0 讨论(0)
  • 2020-12-08 02:41

    if you want change background color on cardView just add this code on your cardview XML

    app:cardBackgroundColor="@color/whatever_color_you"
    
    0 讨论(0)
  • 2020-12-08 02:42

    Make Cardview will host one viewgroup for eg relative layout in this case and then simply set any background to relative layout.

    <?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:id="@+id/list_container"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="5dp"
            card_view:cardCornerRadius="4dp"
            card_view:cardUseCompatPadding="true">
    
            <RelativeLayout
                android:id="@+id/list_container_bg"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center"
                android:orientation="vertical">
    
               <!--Add cardview contents-->
    
            </RelativeLayout>
        </android.support.v7.widget.CardView>
    
    0 讨论(0)
  • 2020-12-08 02:49

    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>
    
    0 讨论(0)
  • 2020-12-08 02:51

    for drawable or color just use:

    cvSellerShopp.setBackgroundResource(R.drawable.ic_action_add_image);
    

    for color use:

     cvSellerShopp.setCardBackgroundColor(R.color.colorPrimary);
    

    but this one does not produce the intended results

    0 讨论(0)
  • 2020-12-08 02:55

    You can use LinearLayout or Relative layout inside CardView and set drawable background it like below :

    <android.support.v7.widget.CardView
        android:id="@+id/card6"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="8dp"
        android:layout_marginTop="10dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/card4"
        app:cardCornerRadius="10dp"
        app:cardElevation="4dp"
        app:cardMaxElevation="8dp">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="@drawable/card_bg"
            android:padding="10dp">
    
            <ImageView
                android:id="@+id/card6Image"
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:layout_gravity="center_horizontal"
                android:src="@mipmap/ic_launcher_round" />
    
            <TextView
                android:id="@+id/card6Title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:padding="5dp"
                android:text="Daily Check In"
                android:textColor="#ffffff"
                android:textSize="15sp" />
    
            <TextView
                android:id="@+id/card6Description"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="5dp"
                android:text="Just Check in Daily and Earn Credits"
                android:textColor="#ffffff"
                android:textSize="10sp" />
        </LinearLayout>
    
    </android.support.v7.widget.CardView>
    

    Below is my Drawable resource file :

    <?xml version="1.0" encoding="UTF-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle" >
    <gradient
        android:angle="90"
        android:centerColor="#555994"
        android:endColor="#b5b6d2"
        android:startColor="#555994"
        android:type="linear" />
    
    <corners
        android:radius="0dp"/>
    

    0 讨论(0)
提交回复
热议问题