1

我已将位图设置为 ImageView,然后想为宽度和高度设置 wrap_content。

imgSubsegment = (ImageView) findViewById(R.id.subsegment);

ViewGroup.LayoutParams imageViewParams = new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);

......................................

imgSubsegmentSensor.setImageBitmap(bmpSubsegmentSensor);
imgSubsegmentSensor.setLayoutParams(imageViewParams);

它在给java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.view.ViewGroup$MarginLayoutParams

我的代码有什么问题?怎么解决?

4

2 回答 2

2
    imgSubsegment = (ImageView) findViewById(R.id.subsegment);

    ViewGroup.MarginLayoutParams imageViewParams = new ViewGroup.MarginLayoutParams(
        ViewGroup.MarginLayoutParams.WRAP_CONTENT,
        ViewGroup.MarginLayoutParams.WRAP_CONTENT);

    ......................................

    imgSubsegmentSensor.setImageBitmap(bmpSubsegmentSensor);
    imgSubsegmentSensor.setLayoutParams(imageViewParams);

试试这个。

于 2012-10-15T13:47:47.363 回答
0
FrameLayout.LayoutParams imageViewParams = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.WRAP_CONTENT,
                FrameLayout.LayoutParams.WRAP_CONTENT);

.................

imgSubsegmentSensor.setImageBitmap(bmpSubsegmentSensor);
    imgSubsegmentSensor.setLayoutParams(imageViewParams);

有用。

于 2012-10-16T05:42:14.233 回答