当我尝试以kitkat中的 CardView
进行 setBackground()
时,我有问题。当应用程序在android上运行时,我的代码没有问题:
Drawable drawable = new BitmapDrawable(appContext.getResources(), bitmap);
if (cardView != null) {
cardView.setBackground(drawable);
}
但是,当它以比Android大的API运行时,我会发现此错误:android.graphics.drawable.BitmapDrawable cannot be cast to android.support.v7.widget.RoundRectDrawableWithShadow.
我发现这是已知的问题,并且您想将颜色设置为卡的背景,但是是否有任何解决方案可以在编程上为背景设置位图吗?您是否知道如何从BitmapDrawable
创建RoundRectDrawableWithShadow
?
您是否知道如何从我的bitmapDrawable中创建RountRectDrawablewithShadow?
除非您想重写自己的CardView
实现。
RoundRectDrawableWithShadow
是一个包裹的私人类,用于摄取由 app:cardBackgroundColor
或 setCardBackgroundColro(int)
提供的 color ,并将其包裹在带有阴影的圆形矩形中,以产生可绘制的可绘图。
由于API 21中引入了高程作为CardView
背景的一部分,因此可以在先前的平台上绘制,因此您无法通过setBackground(*)
。。
是否有任何解决方案可以通过编程为背景设置位图?
您可以将View
,*Layout
等放置在卡中,并在其上设置一个圆形的可绘制背景。不过,这不是有效的性能。
https://developer.android.com/reference/android/support/v4/graphics/drawable/roundededbitmapdrable.html