Android Change the background color of CardView programmatically
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26561122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change the background color of CardView programmatically
提问by Gabriele Mariotti
The CardViewhas an attribute card_view:cardBackgroundColor
to define the background color.
This attribute works fine.
The CardViewhas an attribute card_view:cardBackgroundColor
to define the background color.
This attribute works fine.
At the same time there isn't a method to change the color dynamically.
At the same time there isn't a method to change the color dynamically.
I've just tried solutions like:
I've just tried solutions like:
mCardView.setBackgroundColor(...);
or using a Layout inside the cardView
or using a Layout inside the cardView
<android.support.v7.widget.CardView>
<LinearLayout
android:id="@+id/inside_layout">
</android.support.v7.widget.CardView>
View insideLayout = mCardView.findViewById(R.id.inside_layout);
cardLayout.setBackgroundColor(XXXX);
These solutions don't work because the card has a cardCornerRadius.
These solutions don't work because the card has a cardCornerRadius.
回答by Simon
What you are looking for is:
What you are looking for is:
CardView card = ...
card.setCardBackgroundColor(color);
In XML
In XML
card_view:cardBackgroundColor="@android:color/white"
Update: in XML
Update: in XML
app:cardBackgroundColor="@android:color/white"
回答by user790999
Use the property card_view:cardBackgroundColor:
Use the property card_view:cardBackgroundColor:
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_gravity="center"
card_view:cardCornerRadius="4dp"
android:layout_margin="10dp"
card_view:cardBackgroundColor="#fff"
>
回答by eluleci
You can use this in XML
You can use this in XML
card_view:cardBackgroundColor="@android:color/white"
or this in Java
or this in Java
cardView.setCardBackgroundColor(Color.WHITE);
回答by m.v.n.kalyani
I used this code to set programmatically:
I used this code to set programmatically:
card.setCardBackgroundColor(color);
Or in XML you can use this code:
Or in XML you can use this code:
card_view:cardBackgroundColor="@android:color/white"
回答by Paul Burke
The way it's set in the initialize
method uses the protected RoundRectDrawable
class, like so:
The way it's set in the initialize
method uses the protected RoundRectDrawable
class, like so:
RoundRectDrawable backgroundDrawable = new RoundRectDrawable(backgroundColor, cardView.getRadius());
cardView.setBackgroundDrawable(backgroundDrawable);
It's not pretty, but you can extend that class. Something like:
It's not pretty, but you can extend that class. Something like:
package android.support.v7.widget;
public class MyRoundRectDrawable extends RoundRectDrawable {
public MyRoundRectDrawable(int backgroundColor, float radius) {
super(backgroundColor, radius);
}
}
then:
then:
final MyRoundRectDrawable backgroundDrawable = new MyRoundRectDrawable(bgColor,
mCardView.getRadius());
mCardView.setBackgroundDrawable(backgroundDrawable);
EDIT
EDIT
This won't give you the shadow on < API 21, so you'd have to do the same with RoundRectDrawableWithShadow
.
This won't give you the shadow on < API 21, so you'd have to do the same with RoundRectDrawableWithShadow
.
There doesn't appear to be a better way to do this.
There doesn't appear to be a better way to do this.
回答by kandroidj
A little late here & partially off topic since this is not programmatically but I find it best to setup styles for Widgets and you can do this for a CardView
just create a style it will keep your xml cleaner...
A little late here & partially off topic since this is not programmatically but I find it best to setup styles for Widgets and you can do this for a CardView
just create a style it will keep your xml cleaner...
<style name="MyCardViewStyle" parent="CardView">
<!-- Card background color -->
<item name="cardBackgroundColor">@android:color/white</item>
<!-- Ripple for API 21 of android, and regular selector on older -->
<item name="android:foreground">?android:selectableItemBackground</item>
<!-- Resting Elevation from Material guidelines -->
<item name="cardElevation">2dp</item>
<!-- Add corner Radius -->
<item name="cardCornerRadius">2dp</item>
<item name="android:clickable">true</item>
<item name="android:layout_margin">8dp</item>
</style>
this is using android.support.v7.widget.CardView
this is using android.support.v7.widget.CardView
and then setting the style in the layout file:
and then setting the style in the layout file:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
style="@style/MyCardViewStyle">
<!-- Other fields-->
</android.support.v7.widget.CardView>
you need to import the appcompat-v7 library using Android studio via gradle:
you need to import the appcompat-v7 library using Android studio via gradle:
dependencies {
compile 'com.android.support:appcompat-v7:22.2.0'
}
hope this helps. happy coding
hope this helps. happy coding
回答by Steve
I was having a similar issue with formatting CardViews in a recylerView.
I was having a similar issue with formatting CardViews in a recylerView.
I got this simple solution working, not sure if it's the best solution, but it worked for me.
I got this simple solution working, not sure if it's the best solution, but it worked for me.
mv_cardView.getBackground().setTint(Color.BLUE)
It gets the background Drawable of the cardView and tints it.
It gets the background Drawable of the cardView and tints it.
回答by Aditya Patil
You can use below
You can use below
cardview.setBackgroundColor(Color.parseColor("#EAEDED"));
回答by yakup_y
In JAVA
In JAVA
cardView.setCardBackgroundColor(0xFFFEFEFE);
android use ARGB colors. you can use like this (0xFF + RGB COLOR)--Hard-coded color.
android use ARGB colors. you can use like this (0xFF + RGB COLOR)--Hard-coded color.
回答by Martial Konvi
I came across the same issue while trying to create a cardview programmatically, what is strange is that looking at the doc https://developer.android.com/reference/android/support/v7/widget/CardView.html#setCardBackgroundColor%28int%29, Google guys made public the api to change the background color of a card view but weirdly i didn't succeed to have access to it in the support library, so here is what worked for me:
I came across the same issue while trying to create a cardview programmatically, what is strange is that looking at the doc https://developer.android.com/reference/android/support/v7/widget/CardView.html#setCardBackgroundColor%28int%29, Google guys made public the api to change the background color of a card view but weirdly i didn't succeed to have access to it in the support library, so here is what worked for me:
CardViewBuilder.java
CardViewBuilder.java
mBaseLayout = new FrameLayout(context);
// FrameLayout Params
FrameLayout.LayoutParams baseLayoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
mBaseLayout.setLayoutParams(baseLayoutParams);
// Create the card view.
mCardview = new CardView(context);
mCardview.setCardElevation(4f);
mCardview.setRadius(8f);
mCardview.setPreventCornerOverlap(true); // The default value for that attribute is by default TRUE, but i reset it to true to make it clear for you guys
CardView.LayoutParams cardLayoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
cardLayoutParams.setMargins(12, 0, 12, 0);
mCardview.setLayoutParams(cardLayoutParams);
// Add the card view to the BaseLayout
mBaseLayout.addView(mCardview);
// Create a child view for the cardView that match it's parent size both vertically and horizontally
// Here i create a horizontal linearlayout, you can instantiate the view of your choice
mFilterContainer = new LinearLayout(context);
mFilterContainer.setOrientation(LinearLayout.HORIZONTAL);
mFilterContainer.setPadding(8, 8, 8, 8);
mFilterContainer.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT, Gravity.CENTER));
// And here is the magic to get everything working
// I create a background drawable for this view that have the required background color
// and match the rounded radius of the cardview to have it fit in.
mFilterContainer.setBackgroundResource(R.drawable.filter_container_background);
// Add the horizontal linearlayout to the cardview.
mCardview.addView(mFilterContainer);
filter_container_background.xml
filter_container_background.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="8dp"/>
<solid android:color="@android:color/white"/>
Doing that i succeed in keeping the cardview shadow and rounded corners.
Doing that i succeed in keeping the cardview shadow and rounded corners.