如何在活动android中设置Imageview高度和宽度
嗨,我想在我的活动中更改图像视图的高度和宽度属性 我尝试了以下方式,但它不适合我如何在活动android中设置Imageview高度和宽度,android,height,imageview,Android,Height,Imageview,嗨,我想在我的活动中更改图像视图的高度和宽度属性 我尝试了以下方式,但它不适合我 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;
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的高度和宽度。我有什么错误吗?
如何做到这一点?
需要帮助。。。
谢谢……试试这样的东西
LayoutParams params = new LayoutParams(100, 100);
parantlayout.addView(coupon_img, params);
我想这会对您有所帮助。在这段代码中,我正在运行时创建一个
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);
这将设置图像视图的尺寸。但是,它们将位于px。如果需要dp值,请使用此处的代码:
更新:
要更改已在XML中定义的现有ImageView
的维度,请使用以下命令:
coupon_img.setLayoutParams(new LayoutParams(100, 100));
我认为您没有将更改的图像添加到布局中
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
使用“缩放类型”将图像src调整到图像视图中谢谢Siddharth的帮助。如果我已经在xml文件中定义了图像的高度和宽度,请告诉我如何更改它。@nilkash:hmm。。让我回到办公室,测试一些东西并进行更新。@nilkash:您使用充气布局的ImageView有什么原因吗?我要求我在您当前的参数范围内进行测试。另外,XML中为ImageView设置的默认宽度和高度是多少?@sidhharth感谢您的回复。。我的实际问题是,当我在尺寸为720*1270的设备上测试我的应用程序时(但对于android xhdp,它是960dp x 720dp),它看起来并不好。这就是我想根据屏幕分辨率改变图像大小的原因。@nilkash:那么我建议的任何一种方法现在都有效吗?