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));
app:cardBackgroundColor="#488747"
在您的卡片视图中使用它,您可以更改卡片视图的颜色
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
的 Kotlin
app:cardBackgroundColor="@android:color/red"
代码
cardName.setCardBackgroundColor(ContextCompat.getColor(this, R.color.colorGray));
您可以使用
app:cardBackgroundColor="@color/red"
或
android:backgroundTint="@color/red"
在XML中:
app:cardBackgroundColor="@color/your_color_name"
在 Java 和 Kotlin 中,您都可以通过编程方式完成:
cardView.setCardBackgroundColor(ContextCompat.getColor(this, R.color.your_color_name));
我将 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));
app:cardBackgroundColor="#488747"
在您的卡片视图中使用它,您可以更改卡片视图的颜色
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
的 Kotlinapp:cardBackgroundColor="@android:color/red"
代码
cardName.setCardBackgroundColor(ContextCompat.getColor(this, R.color.colorGray));
您可以使用
app:cardBackgroundColor="@color/red"
或
android:backgroundTint="@color/red"
在XML中:
app:cardBackgroundColor="@color/your_color_name"
在 Java 和 Kotlin 中,您都可以通过编程方式完成:
cardView.setCardBackgroundColor(ContextCompat.getColor(this, R.color.your_color_name));