I am using RecyclerView with GridLayoutManager and I have each item as CardView.
Unfortunately, the CardView here does not seem to change its background color. I tried in layout and programmatically as well but I have tried nothing seems to work.
I Have been struggling for quite a while. I appreciate if someone could help me out with this issue.
If you want to change the card background color, use:
app:cardBackgroundColor="@somecolor"
like this:
<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: As pointed by @imposible, you need to include
xmlns:app="http://schemas.android.com/apk/res-auto"
in your root XML tag in order to make this snippet function
You can do it either in XML or programmatically:
In XML:
card_view:cardBackgroundColor="@android:color/red"
Programmatically:
cardView.setBackgroundColor(ContextCompat.getColor(this, R.color.my_color));
Kotlin for XML
app:cardBackgroundColor="@android:color/red"
code
cardName.setCardBackgroundColor(ContextCompat.getColor(this, R.color.colorGray));
XML code
<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" />
From the code
CardView card = findViewById(R.id.card_view_top);
card.setCardBackgroundColor(Color.parseColor("#E6E6E6"));
In XML:
app:cardBackgroundColor="@color/your_color_name"
Both in Java and Kotlin you can do it programmatically:
cardView.setCardBackgroundColor(ContextCompat.getColor(this, R.color.your_color_name));
If still not visible in design view, you can temporarily remove these lines
app:cardUseCompatPadding="true"
or
card_view:cardUseCompatPadding="true"
If you need them, you can then add them back at build time
If someone still gets the white color instead of the color he picked, just change the emulator you working on, that's works for me
app:cardBackgroundColor="#488747"
在您的卡片视图中使用它,您可以更改卡片视图的颜色
You can use
app:cardBackgroundColor="@color/red"
or
android:backgroundTint="@color/red"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.