0

嗨,我想在我的活动中更改图像视图的高度和宽度属性,我按以下方式尝试了它,但它对我不起作用......

View card_view = getLayoutInflater().inflate(R.layout.card_details1,null);
coupon_img = (ImageView) card_view.findViewById(R.id.coupon_image);
// I tried this ////////
coupon_img.getLayoutParams().height = 20;
coupon_img.getLayoutParams().width = 20;
// I also tried this ////
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(100, 100);
    coupon_img.setLayoutParams(layoutParams);
// also this one ////
coupon_img.setMaxHeight(10);

但我无法更改 imageview src 的高度和宽度。我做错了什么吗?这该怎么做?需要帮助...谢谢...

4

3 回答 3

4

在这段代码中,我ImageView在运行时创建了一个新实例并为其设置维度:

// SET THE IMAGEVIEW DIMENSIONS
int dimens = 120;
float density = activity.getResources().getDisplayMetrics().density;
int finalDimens = (int)(dimens * density);

LinearLayout.LayoutParams imgvwDimens = 
        new LinearLayout.LayoutParams(finalDimens, finalDimens);
imgAlbumPhoto.setLayoutParams(imgvwDimens);

// SET SCALETYPE
imgAlbumPhoto.setScaleType(ScaleType.CENTER_CROP);

// SET THE MARGIN
int dimensMargin = 5;
float densityMargin = activity.getResources().getDisplayMetrics().density;
int finalDimensMargin = (int)(dimensMargin * densityMargin);

LinearLayout.LayoutParams imgvwMargin = 
        new LinearLayout.LayoutParams(finalDimens, finalDimens);
imgvwMargin.setMargins
(finalDimensMargin, finalDimensMargin, finalDimensMargin, finalDimensMargin);

这将设置ImageView. 但是,它们将在px中。如果您需要dp值,请使用此处的代码: https ://stackoverflow.com/a/9563438/450534

更新:

ImageView要更改已在 XML 中定义的现有尺寸,请使用以下命令:

coupon_img.setLayoutParams(new LayoutParams(100, 100));
于 2013-02-21T05:57:02.067 回答
2

尝试这样的事情......

 LayoutParams params = new LayoutParams(100, 100);
 parantlayout.addView(coupon_img, params);

我想这会对你有所帮助。

于 2013-02-21T05:56:21.953 回答
2

我认为您没有将更改后的图像添加到布局中。

LinearLayout ll = (LinearLayout)findViewById(R.layout.yourlinearlayout);

image.setLayoutParams(
          new LinearLayout.LayoutParams(
                 bmp.getWidth(), 
                 bmp.getHeight())); 
ll.addView(image);// Then add the image to linear layout
于 2013-02-21T05:57:41.013 回答