Drawable as Background for CardView

后端 未结 11 961
傲寒
傲寒 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:55

    You have 2 possible solutions.

    1. Setting background programmatically:
    cardView.setBackgroundResource(R.drawable.background)
    
    1. Add some child to cardview that will fill cardview's bounds and set background on it:
    <android.support.v7.widget.CardView
    ...>
         <ConstraintLayout
            android:background="@drawable/background"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </android.support.v7.widget.CardView>
    

    This way you can also give images some shape that you can configure by the shape of cardview. Disadvantage is that it is bad for performance.

    0 讨论(0)
  • 2020-12-08 03:01

    I got this working by adding a linearlayout in the cardview and then setting cardPreventCornerOverlap to false in the cardview.

       <android.support.v7.widget.CardView
        android:id="@+id/result_cv"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_margin="4dp"
        app:cardCornerRadius="16dp"
        app:cardElevation="8dp"
        app:cardPreventCornerOverlap="false"
        >
    
         <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/gradient"
            android:gravity="center"
            >
    
           <!-- your views -->
    
        </LinearLayout>
    
        </android.support.v7.widget.CardView>
    
    0 讨论(0)
  • 2020-12-08 03:02

    Try this

    <?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="match_parent"
            android:background="@drawable/yourbackground"
            android:layout_height="match_parent">
    
           <!--Add cardview contents-->
    
        </RelativeLayout>
    </android.support.v7.widget.CardView>
    

    card_view:cardBackgroundColor="@android:color/holo_green_dark

    here you can set any color or make it transparent by giving color transparent and if u wand some drawable like image or svg put RelativeLayout background

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

    You can simply use the below line in your xml file.

    app:cardBackgroundColor="@drawable/your_custom_theme_here"
    
    0 讨论(0)
  • 2020-12-08 03:06

    you can set foreground as follow for the card view to set custom bg

    android:foreground="@drawable/bg"
    

    and here is the transparent bg.xml

    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <solid android:color="@android:color/transparent" />
        <stroke
            android:width="1dp"
            android:color="#d9d9d9" />
        <corners android:radius="4dp" />
        <padding
            android:bottom="0dp"
            android:left="0dp"
            android:right="0dp"
            android:top="0dp" />
    </shape>
    
    0 讨论(0)
提交回复
热议问题