android card设置背景,android - 以编程方式更改CardView的背景颜色

android - 以编程方式更改CardView的背景颜色

CardView具有属性card_view:cardBackgroundColor以定义背景颜色。这个属性工作正常。

同时,没有一种动态改变颜色的方法。

我刚刚尝试过以下解决方案:

mCardView.setBackgroundColor(...);

或使用cardView内的布局

android:id="@+id/inside_layout">

View insideLayout = mCardView.findViewById(R.id.inside_layout);

cardLayout.setBackgroundColor(XXXX);

这些解决方案不起作用,因为该卡具有cardCornerRadius。

13个解决方案

227 votes

你在寻找的是:

CardView card = ...

card.setCardBackgroundColor(color);

在XML中

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

Simon Heinen answered 2019-07-26T03:54:01Z

117 votes

使用属性card_view:cardBackgroundColor:

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"

>

user790999 answered 2019-07-26T03:54:24Z

21 votes

您可以在XML中使用它

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

或者用Java编写

cardView.setCardBackgroundColor(Color.WHITE);

eluleci answered 2019-07-26T03:54:55Z

17 votes

我使用此代码以编程方式设置:

card.setCardBackgroundColor(color);

或者在XML中,您可以使用以下代码:

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

m.v.n.kalyani answered 2019-07-26T03:55:25Z

9 votes

这里有点晚了& 部分偏离主题,因为这不是以编程方式,但我发现最好为小部件设置样式,你可以为android.support.v7.widget.CardView做这个只是创建一个风格,它将保持你的xml清洁...

@android:color/white

?android:selectableItemBackground

2dp

2dp

true

8dp

这是使用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">

你需要通过gradle使用Android studio导入appcompat-v7库:

dependencies {

compile 'com.android.support:appcompat-v7:22.2.0'

}

希望这可以帮助。 快乐的编码

kandroidj answered 2019-07-26T03:56:16Z

8 votes

它在RoundRectDrawableWithShadow方法中设置的方式使用受保护的RoundRectDrawable类,如下所示:

RoundRectDrawable backgroundDrawable = new RoundRectDrawable(backgroundColor, cardView.getRadius());

cardView.setBackgroundDrawable(backgroundDrawable);

它不漂亮,但你可以扩展那个类。 就像是:

package android.support.v7.widget;

public class MyRoundRectDrawable extends RoundRectDrawable {

public MyRoundRectDrawable(int backgroundColor, float radius) {

super(backgroundColor, radius);

}

}

然后:

final MyRoundRectDrawable backgroundDrawable = new MyRoundRectDrawable(bgColor,

mCardView.getRadius());

mCardView.setBackgroundDrawable(backgroundDrawable);

编辑

这不会给你影子< API 21,所以你必须对RoundRectDrawableWithShadow做同样的事情。

似乎没有更好的方法来做到这一点。

Paul Burke answered 2019-07-26T03:57:04Z

3 votes

我在recylerView中格式化CardView时遇到了类似的问题。

我得到了这个简单的解决方案,不确定它是否是最好的解决方案,但它对我有用。

mv_cardView.getBackground().setTint(Color.BLUE)

它获取了cardView的背景Drawable并对其进行着色。

Steve answered 2019-07-26T03:57:43Z

2 votes

在尝试以编程方式创建cardview时遇到了同样的问题,奇怪的是查看doc [https://developer.android.com/reference/android/support/v7/widget/CardView.html#setCardBackgroundColor% 28int%29,]谷歌人公开api改变卡片视图的背景颜色,但奇怪的是我没有成功在支持库中访问它,所以这对我有用:

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

这样做我成功地保持了cardview阴影和圆角。

Martial Konvi answered 2019-07-26T03:58:29Z

2 votes

在JAVA

cardView.setCardBackgroundColor(0xFFFEFEFE);

android使用ARGB颜色。 你可以这样使用(0xFF + RGB COLOR) - 硬编码颜色。

yakup_y answered 2019-07-26T03:59:00Z

2 votes

你可以在java中使用它。

cardView.setCardBackgroundColor(Color.parseColor( “#cac8a0”));

代码颜色表[http://www.color-hex.com/]

Muthu Krishnan answered 2019-07-26T03:59:38Z

0 votes

试试它很容易

card_view:cardBackgroundColor="#fff"

card_view:cardCornerRadius="9dp"

card_view:cardElevation="4dp"

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

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:paddingTop="10dp"

android:paddingBottom="10dp"

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

Singupurapu prudhvi raj answered 2019-07-26T04:00:04Z

0 votes

我在Xamarin.Android上遇到了同样的问题 - VS(2017)

适合我的解决方案:

在您的XML文件中添加:

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

并在您的cardview元素中添加此适当性:

card_view:cardBackgroundColor="#ffb4b4"

(即)

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal"

android:layout_margin="12dp"

card_view:cardCornerRadius="4dp"

card_view:cardElevation="1dp"

card_view:cardPreventCornerOverlap="false"

card_view:cardBackgroundColor="#ffb4b4" />

您还可以添加cardview和cardElevation。

如果要以编程方式编辑cardview,只需使用以下代码:对于(C#)

cvBianca = FindViewById(Resource.Id.cv_bianca);

cvBianca.Elevation = 14;

cvBianca.Radius = 14;

cvBianca.PreventCornerOverlap = true;

cvBianca.SetCardBackgroundColor(Color.Red);

现在,您可以以编程方式更改背景颜色,而不会丢失边框,边角半径和高程。

Fabio answered 2019-07-26T04:01:20Z

0 votes

R.color有点腼腆。 我的结构中有颜色列表,模型就像

class ModelColor : Serializable {

var id: Int? = 0

var title: String? = ""

var color: Int? = 0// HERE IS THE COLOR FIELD WE WILL USE

constructor(id: Int?, title: String?, color: Int?) {

this.id = id

this.title = title

this.color = color

}

}

用颜色加载模型,结构上的最后一项取自R.color

list.add(ModelColor(2, getString(R.string.orange), R.color.orange_500))

最后你可以setBackgrıundResource

cv_add_goal_choose_color.setBackgroundResource(color)

Sam answered 2019-07-26T04:02:01Z

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值