简体   繁体   English

CardView 背景颜色始终为白色

[英]CardView background color always white

I am using RecyclerView with GridLayoutManager and I have each item as CardView.我将 RecyclerView 与 GridLayoutManager 一起使用,并且我将每个项目都作为 CardView。

Unfortunately, the CardView here does not seem to change its background color.不幸的是,这里的 CardView 似乎并没有改变它的背景颜色。 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编辑:正如@imposible 所指出的,你需要包括

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

in your root XML tag in order to make this snippet function在您的根 XML 标记中,以使此代码段起作用

You can do it either in XML or programmatically:您可以使用 XML 或以编程方式执行此操作:

In XML:在 XML 中:

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

Programmatically:以编程方式:

cardView.setBackgroundColor(ContextCompat.getColor(this, R.color.my_color));

Kotlin for XML用于XML 的Kotlin

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

code代码

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

XML 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" />

From the code从代码

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

In XML:在 XML 中:

app:cardBackgroundColor="@color/your_color_name"

Both in Java and Kotlin you can do it programmatically:JavaKotlin 中,您都可以通过编程方式完成:

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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM