[英]How to change background color of CardView programmatically
Google 决定不创建动态更改 CardView 背景颜色的方法是否有原因?
剪在这里
变通方法
@Justin Powell 建议的简单代码行对我不起作用。 在 Android 5.0 上。 但它确实让我朝着正确的方向前进。
此代码,(MyRoundRectDrawableWithShadow是副本这个)
card.setBackgroundDrawable(new MyRoundRectDrawableWithShadow(context.getResources(),
color,
card.getRadius(),
card.getCardElevation(),
card.getMaxCardElevation()));
...给了我这个错误,
java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.app.MyRoundRectDrawableWithShadow$RoundRectHelper.drawRoundRect(android.graphics.Canvas, android.graphics.RectF, float, android.graphics.Paint)' on a null object reference
at com.example.app.MyRoundRectDrawableWithShadow.draw(MyRoundRectDrawableWithShadow.java:172)
这只是说有一个被调用的接口为空。 然后我查看了CardView 源代码,以了解它是如何做到的。 我发现下面这段代码以某种静态的方式初始化了界面(我不太明白为什么,如果你知道请解释一下),然后我在类init中调用一次,然后你可以设置颜色的卡片用上面的代码块。
final RectF sCornerRect = new RectF();
MyRoundRectDrawableWithShadow.sRoundRectHelper
= new MyRoundRectDrawableWithShadow.RoundRectHelper() {
@Override
public void drawRoundRect(Canvas canvas, RectF bounds, float cornerRadius,
Paint paint) {
final float twoRadius = cornerRadius * 2;
final float innerWidth = bounds.width() - twoRadius;
final float innerHeight = bounds.height() - twoRadius;
sCornerRect.set(bounds.left, bounds.top,
bounds.left + cornerRadius * 2, bounds.top + cornerRadius * 2);
canvas.drawArc(sCornerRect, 180, 90, true, paint);
sCornerRect.offset(innerWidth, 0);
canvas.drawArc(sCornerRect, 270, 90, true, paint);
sCornerRect.offset(0, innerHeight);
canvas.drawArc(sCornerRect, 0, 90, true, paint);
sCornerRect.offset(-innerWidth, 0);
canvas.drawArc(sCornerRect, 90, 90, true, paint);
//draw top and bottom pieces
canvas.drawRect(bounds.left + cornerRadius, bounds.top,
bounds.right - cornerRadius, bounds.top + cornerRadius,
paint);
canvas.drawRect(bounds.left + cornerRadius,
bounds.bottom - cornerRadius, bounds.right - cornerRadius,
bounds.bottom, paint);
//center
canvas.drawRect(bounds.left, bounds.top + cornerRadius,
bounds.right, bounds.bottom - cornerRadius, paint);
}
};
然而,这个解决方案确实产生了一个新问题。 不确定在棒棒糖之前会发生什么,但是当 CardView 首次初始化时,它似乎根据您在 XML 中设置的属性创建了一个 RoundRectDrawable 作为背景。 当我们用上面的代码改变颜色时,我们设置了一个 MyRoundRectDrawableWithShadow 作为背景,如果你想再次改变颜色,card.getRadius()、card.getCardElevation() 等将不再起作用。
因此,这首先尝试将它从 CardView 获取的背景解析为 MyRoundRectDrawableWithShadow,如果成功则从中获取值(第二次更改颜色时,它将从中获取值)。 但是,如果它失败(这将在第一次颜色更改时发生,因为背景是不同的类)它将直接从 CardView 本身获取值。
float cardRadius;
float maxCardElevation;
try{
MyRoundRectDrawableWithShadow background = (MyRoundRectDrawableWithShadow)card.getBackground();
cardRadius = background.getCornerRadius();
maxCardElevation = background.getMaxShadowSize();
}catch (ClassCastException classCastExeption){
cardRadius = card.getRadius();
maxCardElevation = card.getMaxCardElevation();
}
card.setBackgroundDrawable(
new MyRoundRectDrawableWithShadow(context.getResources(),
Color.parseColor(note.getColor()),
cardRadius,
card.getCardElevation(),
maxCardElevation));
希望这是有道理的,我的母语不是英语......如前所述,这仅在 Lollipop 上进行了测试。
只是为了更新:最新的支持库提供了一个直接的功能:
CardView cardView;
cardView.setCardBackgroundColor(color);
我不知道有什么特别的理由。
但是,如果您有兴趣绕过这个遗漏......
CardView 对这个属性所做的一切就是使用颜色创建一个可绘制的圆角矩形,然后将其指定为 CardView 的背景。 如果您真的想以编程方式设置颜色,您可以创建RoundRectDrawableWithShadow的副本,然后执行以下操作:
mCardView.setBackgroundDrawable(new MyRoundRectDrawableWithShadow(getResources(), color, radius));
您不能继承 RoundRectDrawableWithShadow或直接使用它,因为它不是公共的。
这很棘手。 您需要破解 API 才能实现此功能。 @Justin Powell 的回答是正确的,但在 API 21 中崩溃了。我的解决方案解决了这个问题。 您需要添加这两个类:
MyRoundRectDrawableWithShadow
:
package android.support.v7.widget;
import android.content.res.Resources;
public class MyRoundRectDrawableWithShadow extends RoundRectDrawableWithShadow {
public MyRoundRectDrawableWithShadow(Resources resources, int backgroundColor) {
super(resources, backgroundColor,
resources.getDimensionPixelSize(R.dimen.cardview_default_radius),
resources.getDimensionPixelSize(R.dimen.cardview_default_elevation),
resources.getDimensionPixelSize(R.dimen.cardview_default_elevation));
}
public MyRoundRectDrawableWithShadow(Resources resources, int backgroundColor, float radius) {
super(resources, backgroundColor, radius,
resources.getDimensionPixelSize(R.dimen.cardview_default_elevation),
resources.getDimensionPixelSize(R.dimen.cardview_default_elevation));
}
public MyRoundRectDrawableWithShadow(Resources resources, int backgroundColor, float radius,
float shadowSize, float maxShadowSize) {
super(resources, backgroundColor, radius, shadowSize, maxShadowSize);
}
}
MyRoundRectDrawable
:
package android.support.v7.widget;
import android.content.res.Resources;
public class MyRoundRectDrawable extends RoundRectDrawable {
public MyRoundRectDrawable(Resources resources, int backgroundColor) {
super(backgroundColor, resources.getDimensionPixelSize(R.dimen.cardview_default_radius));
}
public MyRoundRectDrawable(int backgroundColor, float radius) {
super(backgroundColor, radius);
}
}
然后使用此代码更改背景颜色:
final Drawable background;
if (Build.VERSION.SDK_INT >= 21) {
background = new MyRoundRectDrawable(color);
} else {
background = new MyRoundRectDrawableWithShadow(resources, color);
}
// This is to avoid to use a deprecated method
if (Build.VERSION.SDK_INT >= 16) {
cardView.setBackground(background);
} else {
cardView.setBackgroundDrawable(background);
}
只需编写此代码段: cardView.setCardBackgroundColor(getResources().getColor(R.color.colorPrimary));
你想要代码的地方
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.