Android RatingBar:getProgressDrawable 未向右投射 Class
Android RatingBar: getProgressDrawable doesn't cast to right Class
我正在使用
Android RatingBar change star colors
这行代码
LayerDrawable stars = (LayerDrawable) rating_bar.getProgressDrawable();
在我的 Nexus 4 模拟器上运行良好 API v21。
在我的 Galaxy S4 API v19 上尝试相同的代码时,我收到以下错误消息:
Caused by: java.lang.ClassCastException: android.support.v7.internal.widget.TintDrawableWrapper cannot be cast to android.graphics.drawable.LayerDrawable
发生了什么事?
我正在使用最新的构建工具 22 进行构建,我的目标 API 也是 22。
LayerDrawable 用于 API v21.
错误消息中清楚地解释了发生的情况:
TintDrawableWrapper cannot be cast to android.graphics.drawable.LayerDrawable
支持库创建了一个包装器来处理追溯兼容性,您只需要考虑它即可。查看 lib 实现,它可能包含 LayerDrawable
。
您需要按照以下方式做一些事情:
LayerDrawable stars = getProgressDrawable(rating_bar);
}
private LayerDrawable getProgressDrawable(RatingBar ratingBar) {
if (Build.VERSION >= LOLLIPOP) {
return (LayerDrawable) ratingBar.getProgressDrawable();
} else {
// fetch the LayerDrawable based on Google's support implementation,
// probably a simple findViewById()
}
}
创建 RatingDialog 的子class 并使用它代替android.widget.RatingBar
对我有用。这是我使用的代码。
子class:
public class RatingBar extends android.widget.RatingBar {
public RatingBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public RatingBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
}
然后在我的布局中:
<your.package.RatingBar
android:id="@+id/rating_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
/>
然后在 classes 中你要转换 class,只需使用 your.package.RatingBar
而不是 android.widget.RatingBar
今天以编程方式创建 RatingBar 时遇到了同样的问题。以下解决方法也适用于使用 AppCompatRatingBar 的 Android 4.x 设备:
AppCompatRatingBar rb = new AppCompatRatingBar(activity);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.CENTER;
rb.setLayoutParams(layoutParams);
rb.setStepSize(1.0f);
rb.setMax(5);
LayerDrawable layerDrawable = null;
if (rb.getProgressDrawable() instanceof LayerDrawable) {
layerDrawable = (LayerDrawable) rb.getProgressDrawable();
} else if (rb.getProgressDrawable() instanceof DrawableWrapper) {
DrawableWrapper wrapper = (DrawableWrapper) rb.getProgressDrawable();
if (wrapper.getWrappedDrawable() instanceof LayerDrawable) {
layerDrawable = (LayerDrawable) wrapper.getWrappedDrawable();
}
}
if (layerDrawable != null) {
layerDrawable.getDrawable(2).setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
layerDrawable.getDrawable(0).setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
layerDrawable.getDrawable(1).setColorFilter(ContextCompat.getColor(activity, R.color.primary), PorterDuff.Mode.SRC_ATOP);
}
请尝试这个技巧,通过将 getCurrent() 方法添加到 return 可绘制对象,它会正常工作。
RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar);
LayerDrawable 星星 = (LayerDrawable) ratingBar.getProgressDrawable().getCurrent();
stars.getDrawable(2).setColorFilter(getResources().getColor(R.color.colorPrimary), PorterDuff.Mode.SRC_ATOP);
stars.getDrawable(0).setColorFilter(getResources().getColor(R.color.white), PorterDuff.Mode.SRC_ATOP);
stars.getDrawable(1).setColorFilter(getResources().getColor(R.color.colorPrimary), PorterDuff.Mode.SRC_ATOP);
我正在使用
Android RatingBar change star colors
这行代码
LayerDrawable stars = (LayerDrawable) rating_bar.getProgressDrawable();
在我的 Nexus 4 模拟器上运行良好 API v21。 在我的 Galaxy S4 API v19 上尝试相同的代码时,我收到以下错误消息:
Caused by: java.lang.ClassCastException: android.support.v7.internal.widget.TintDrawableWrapper cannot be cast to android.graphics.drawable.LayerDrawable
发生了什么事?
我正在使用最新的构建工具 22 进行构建,我的目标 API 也是 22。
LayerDrawable 用于 API v21.
错误消息中清楚地解释了发生的情况:
TintDrawableWrapper cannot be cast to android.graphics.drawable.LayerDrawable
支持库创建了一个包装器来处理追溯兼容性,您只需要考虑它即可。查看 lib 实现,它可能包含 LayerDrawable
。
您需要按照以下方式做一些事情:
LayerDrawable stars = getProgressDrawable(rating_bar);
}
private LayerDrawable getProgressDrawable(RatingBar ratingBar) {
if (Build.VERSION >= LOLLIPOP) {
return (LayerDrawable) ratingBar.getProgressDrawable();
} else {
// fetch the LayerDrawable based on Google's support implementation,
// probably a simple findViewById()
}
}
创建 RatingDialog 的子class 并使用它代替android.widget.RatingBar
对我有用。这是我使用的代码。
子class:
public class RatingBar extends android.widget.RatingBar {
public RatingBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public RatingBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
}
然后在我的布局中:
<your.package.RatingBar
android:id="@+id/rating_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
/>
然后在 classes 中你要转换 class,只需使用 your.package.RatingBar
而不是 android.widget.RatingBar
今天以编程方式创建 RatingBar 时遇到了同样的问题。以下解决方法也适用于使用 AppCompatRatingBar 的 Android 4.x 设备:
AppCompatRatingBar rb = new AppCompatRatingBar(activity);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.CENTER;
rb.setLayoutParams(layoutParams);
rb.setStepSize(1.0f);
rb.setMax(5);
LayerDrawable layerDrawable = null;
if (rb.getProgressDrawable() instanceof LayerDrawable) {
layerDrawable = (LayerDrawable) rb.getProgressDrawable();
} else if (rb.getProgressDrawable() instanceof DrawableWrapper) {
DrawableWrapper wrapper = (DrawableWrapper) rb.getProgressDrawable();
if (wrapper.getWrappedDrawable() instanceof LayerDrawable) {
layerDrawable = (LayerDrawable) wrapper.getWrappedDrawable();
}
}
if (layerDrawable != null) {
layerDrawable.getDrawable(2).setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
layerDrawable.getDrawable(0).setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
layerDrawable.getDrawable(1).setColorFilter(ContextCompat.getColor(activity, R.color.primary), PorterDuff.Mode.SRC_ATOP);
}
请尝试这个技巧,通过将 getCurrent() 方法添加到 return 可绘制对象,它会正常工作。
RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar);
LayerDrawable 星星 = (LayerDrawable) ratingBar.getProgressDrawable().getCurrent();
stars.getDrawable(2).setColorFilter(getResources().getColor(R.color.colorPrimary), PorterDuff.Mode.SRC_ATOP); stars.getDrawable(0).setColorFilter(getResources().getColor(R.color.white), PorterDuff.Mode.SRC_ATOP); stars.getDrawable(1).setColorFilter(getResources().getColor(R.color.colorPrimary), PorterDuff.Mode.SRC_ATOP);