繁体   English   中英

CardView 背景颜色始终为白色

[英]CardView background color always white

我将 RecyclerView 与 GridLayoutManager 一起使用,并且我将每个项目都作为 CardView。

不幸的是,这里的 CardView 似乎并没有改变它的背景颜色。 我也尝试过布局和编程,但我尝试过似乎没有任何效果。

我已经挣扎了好一阵子了。 如果有人可以帮助我解决这个问题,我将不胜感激。

如果要更改卡片背景颜色,请使用:

app:cardBackgroundColor="@somecolor"

像这样:

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

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

编辑:正如@imposible 所指出的,你需要包括

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

在您的根 XML 标记中,以使此代码段起作用

您可以使用 XML 或以编程方式执行此操作:

在 XML 中:

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

以编程方式:

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

用于XML 的Kotlin

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

代码

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

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

从代码

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

在 XML 中:

app:cardBackgroundColor="@color/your_color_name"

JavaKotlin 中,您都可以通过编程方式完成:

cardView.setCardBackgroundColor(ContextCompat.getColor(this, R.color.your_color_name));

如果在设计视图中仍然不可见,您可以暂时删除这些行

app:cardUseCompatPadding="true"

或者

card_view:cardUseCompatPadding="true"

如果您需要它们,您可以在构建时将它们添加回来

如果有人仍然得到白色而不是他选择的颜色,只需更改您正在使用的模拟器,这对我有用

app:cardBackgroundColor="#488747"

在您的卡片视图中使用它,您可以更改卡片视图的颜色

您可以使用

app:cardBackgroundColor="@color/red"

或者

android:backgroundTint="@color/red"

暂无
暂无

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

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