Android 我应该使用什么样的布局参数?

Android 我应该使用什么样的布局参数?,android,android-layout,Android,Android Layout,问题描述: 我有一个自定义的图像视图: public class TestImageView extends ImageView { ... public void onImageLoaded(Bitmap bitmap) { int width = DeviceInfo.getScreenWidth(); int height = width * (float) bitmap.getHeight() / (float) bitmap.getWidth()

问题描述:

我有一个自定义的图像视图:

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类似乎相同?我不认为有必要。

ViewGroup的每个子类都有自己的LayoutParams(从ViewGroup.MarginLayoutParams派生而来,它是ViewGroup.LayoutParams的后代(每个子类都没有MarginLayoutParams,因为边距设置功能可以通过继承获得)),因为它们支持不同的品质。例如,LinearLayout.LayoutParams支持使用重力。因此,根据ImageView所在的视图组的类型,LayoutParams必须是相同的类型,或者至少强制转换为相同的类型。你有两个选择。一种是在ImageView的子类中假设您将始终使用某种类型的布局,然后将ViewGroup.MarginLayoutParams替换为(您的布局类型).LayoutParams。但如果您改变了这一点,它将停止工作,因此您可以通过使用泛型来避免所有这些:

public class TestImageView<I extends ViewGroup.LayoutParams> extends ImageView {
    public void onImageLoaded(Bitmap bitmap) {
        int width = DeviceInfo.getScreenWidth();
        int height = width * (float) bitmap.getHeight() / (float) bitmap.getWidth();
        I layoutParams = (I)
                new ViewGroup.LayoutParams(width, height);
        this.setLayoutParams(layoutParams);
    }
}
公共类TestImageView扩展了ImageView{
已加载公用无效onImageLoaded(位图){
int width=DeviceInfo.getScreenWidth();
int height=width*(float)bitmap.getHeight()/(float)bitmap.getWidth();
I布局参数=(I)
新建ViewGroup.LayoutParams(宽度、高度);
此.setLayoutParams(layoutParams);
}
}
及其用途:

// using a RelativeLayout
TestImageView<RelativeLayout.LayoutParams> iv = new TestImageView<>();
//使用RelativeLayout
TestImageView iv=新的TestImageView();

我希望这对你有帮助!(不过,我目前没有测试设备,如果它不工作,请发表评论)

ViewGroup的每个子类都有自己的LayoutParams(从ViewGroup.MarginLayoutParams派生而来)(每个子类都没有MarginLayoutParams,因为边距设置功能可以通过继承获得)),因为它们支持不同的质量。例如,LinearLayout.LayoutParams支持重力的使用。因此,根据ImageView所在的视图组的类型,LayoutParams必须是相同的类型,或者至少强制转换为相同的类型。您有两个选项。一个是在使用Imag创建的子类中假设查看您将始终使用某种类型的布局,然后将ViewGroup.MarginLayoutParams替换为(您的布局类型).LayoutParams。但是,如果您更改了它,它将停止工作,因此您可以使用泛型来避免所有这一切:

public class TestImageView<I extends ViewGroup.LayoutParams> extends ImageView {
    public void onImageLoaded(Bitmap bitmap) {
        int width = DeviceInfo.getScreenWidth();
        int height = width * (float) bitmap.getHeight() / (float) bitmap.getWidth();
        I layoutParams = (I)
                new ViewGroup.LayoutParams(width, height);
        this.setLayoutParams(layoutParams);
    }
}
公共类TestImageView扩展了ImageView{
已加载公用无效onImageLoaded(位图){
int width=DeviceInfo.getScreenWidth();
int height=width*(float)bitmap.getHeight()/(float)bitmap.getWidth();
I布局参数=(I)
新建ViewGroup.LayoutParams(宽度、高度);
此.setLayoutParams(layoutParams);
}
}
及其用途:

// using a RelativeLayout
TestImageView<RelativeLayout.LayoutParams> iv = new TestImageView<>();
//使用RelativeLayout
TestImageView iv=新的TestImageView();

我希望这能帮到你!(不过我目前还没有测试设备,如果它不起作用,请发表评论)

ViewGroup的每个子类都有自己的LayoutParams(从ViewGroup.MarginLayoutParams(从ViewGroup.LayoutParams派生而来)(每个子类都没有MarginLayoutParams,因为边距设置功能可以通过继承获得)),因为它们支持不同的质量。例如,LinearLayout.LayoutParams支持重力的使用。因此,根据ImageView所在的视图组的类型,LayoutParams必须是相同的类型,或者至少强制转换为相同的类型。您有两个选项。一个是在使用Imag创建的子类中假设查看您将始终使用某种类型的布局,然后将ViewGroup.MarginLayoutParams替换为(您的布局类型).LayoutParams。但是,如果您更改了它,它将停止工作,因此您可以使用泛型来避免所有这一切:

public class TestImageView<I extends ViewGroup.LayoutParams> extends ImageView {
    public void onImageLoaded(Bitmap bitmap) {
        int width = DeviceInfo.getScreenWidth();
        int height = width * (float) bitmap.getHeight() / (float) bitmap.getWidth();
        I layoutParams = (I)
                new ViewGroup.LayoutParams(width, height);
        this.setLayoutParams(layoutParams);
    }
}
公共类TestImageView扩展了ImageView{
已加载公用无效onImageLoaded(位图){
int width=DeviceInfo.getScreenWidth();
int height=width*(float)bitmap.getHeight()/(float)bitmap.getWidth();
I布局参数=(I)
新建ViewGroup.LayoutParams(宽度、高度);
此.setLayoutParams(layoutParams);
}
}
及其用途:

// using a RelativeLayout
TestImageView<RelativeLayout.LayoutParams> iv = new TestImageView<>();
//使用RelativeLayout
TestImageView iv=新的TestImageView();

我希望这能帮到你!(不过我目前还没有测试设备,如果它不起作用,请发表评论)

ViewGroup的每个子类都有自己的LayoutParams(从ViewGroup.MarginLayoutParams(从ViewGroup.LayoutParams派生而来)(每个子类都没有MarginLayoutParams,因为边距设置功能可以通过继承获得)),因为它们支持不同的质量。例如,LinearLayout.LayoutParams支持重力的使用。因此,根据ImageView所在的视图组的类型,LayoutParams必须是相同的类型,或者至少强制转换为相同的类型。您有两个选项。一个是在使用Imag创建的子类中假设查看您将始终使用某种类型的布局,然后将ViewGroup.MarginLayoutParams替换为(您的布局类型).LayoutParams。但是,如果您更改了它,它将停止工作,因此您可以使用泛型来避免所有这一切:

public class TestImageView<I extends ViewGroup.LayoutParams> extends ImageView {
    public void onImageLoaded(Bitmap bitmap) {
        int width = DeviceInfo.getScreenWidth();
        int height = width * (float) bitmap.getHeight() / (float) bitmap.getWidth();
        I layoutParams = (I)
                new ViewGroup.LayoutParams(width, height);
        this.setLayoutParams(layoutParams);
    }
}
公共类Tes