web-dev-qa-db-fra.com

Couleur de fond de CardView toujours blanche

J'utilise RecyclerView avec GridLayoutManager et chaque élément correspond à CardView.

Malheureusement, le CardView ici ne semble pas changer sa couleur de fond. J'ai essayé la mise en page et la programmation aussi mais je n'ai rien essayé qui semble fonctionner.

Je lutte depuis un certain temps. J'apprécie que quelqu'un puisse m'aider avec ce problème.

85
Ishaan

Si vous voulez changer la couleur de fond de la carte, utilisez: 

app:cardBackgroundColor="@somecolor"

comme ça:

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

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

Edit: Comme indiqué par @imposible, vous devez inclure 

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

dans votre balise XML racine afin de rendre cette fonction d'extrait

205

Vous pouvez le faire en XML ou par programme:

En XML:

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

Par programme:

cardView.setCardBackgroundColor(Color.RED);
25

Code XML

<Android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.Android.com/apk/res-auto"
        Android:id="@+id/card_view_top"
        Android:layout_width="wrap_content"
        Android:layout_height="wrap_content"
        app:cardCornerRadius="5dp"
        app:contentPadding="25dp"
        app:cardBackgroundColor="#e4bfef"
        app:cardElevation="4dp"
        app:cardMaxElevation="6dp" />

Du code

CardView card = findViewById(R.id.card_view_top);
card.setCardBackgroundColor(Color.parseColor("#E6E6E6"));
2
yoAlex5

Kotlin pourXML

app:cardBackgroundColor="@Android:color/red"

code

cardName.setCardBackgroundColor(ContextCompat.getColor(this, R.color.colorGray))

0
Munna Mohammed

app:cardBackgroundColor="#488747"

utilisez ceci dans votre vue de carte et vous pouvez changer une couleur de votre vue de carte

0
BlackBlind567