【发布时间】:2014-04-21 23:20:22
【问题描述】:
问题描述:
我有一个自定义的 ImageView:
public class TestImageView extends ImageView {
...
public void onImageLoaded(Bitmap bitmap) {
int width = DeviceInfo.getScreenWidth();
int height = width * (float) bitmap.getHeight() / (float) bitmap.getWidth();
ViewGroup.MarginLayoutParams layoutParams =
new ViewGroup.MarginLayoutParams(width, height);
this.setLayoutParams(layoutParams);
}
}
然后我收到了这个错误:
java.lang.ClassCastException: android.view.ViewGroup$MarginLayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams
在我的项目中,我确实将 TestImageView 的一个实例作为 FrameLayout 的子级。因此,如果我将 ViewGroup.MarginLayoutParams 更改为 FrameLayout.LayoutParams 它将起作用。但是这个定制的 ImageView 可以在其他类型的布局中,比如 LinearLayout。我认为我不应该在这里将其限制为 FrameLayout.layoutParams。
那么我应该在这里做什么?更一般地说,为什么 Android 会制作不同的 LayoutParams? LayoutParams 类似乎相同?我认为没有必要。
【问题讨论】:
-
你试过使用 LinearLayout.LayoutParams 吗?