我使用RecyclerView
和GridLayoutManager
,并将每个项目作为CardView
。
不幸的是,这里的CardView
似乎无法更改其背景颜色。我已经尝试了布局和编程方式,但好像什么都没用。
我已经苦苦挣扎了一段时间。如果有人能帮我解决这个问题,我将不胜感激。
我使用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标记中添加此代码
android.support.v7.widget.CardView
。对我来说,使用 app:cardBackgroundColor="@somecolor"
是有效的,但是 card_view:cardBackgroundColor="@somecolor"
不行。 - Zamrony P. Juhara您可以使用XML或编程方式完成:
在XML中:
card_view:cardBackgroundColor="@android:color/red"
以编程方式:
cardView.setBackgroundColor(ContextCompat.getColor(this, R.color.my_color));
Kotlin 用于 XML
app:cardBackgroundColor="@android:color/red"
以编程方式:
cardName.setCardBackgroundColor(ContextCompat.getColor(this, R.color.colorGray));
Android CardView 背景颜色
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"));
app:cardBackgroundColor="@color/your_color_name"
在Java和Kotlin中,你都可以通过编程实现:
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"