CardView background color always white

限于喜欢 提交于 2020-06-22 05:01:05

问题


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.


回答1:


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




回答2:


You can do it either in XML or programmatically:

In XML:

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

Programmatically:

cardView.setCardBackgroundColor(Color.RED);



回答3:


Kotlin for XML

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

code

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




回答4:


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"));



回答5:


app:cardBackgroundColor="#488747"

use this in your card view and you can change a color of your card view




回答6:


You can use

app:cardBackgroundColor="@color/red"

or

android:backgroundTint="@color/red"


来源:https://stackoverflow.com/questions/41552973/cardview-background-color-always-white

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!