在使用imageview控件时,由于图片大小不一致,需要给图片设置个最大宽高。设置后的代码如下:
LinearLayout ll = new LinearLayout(ConsumeInfo.this);
ll.setOrientation(LinearLayout.HORIZONTAL);
//设置小图标
ImageView imageView = new ImageView(ConsumeInfo.this);
Bitmap bitmap = BitmapFactory.decodeFile((String)mData.get(position).get("imgpath"));
imageView.setImageBitmap(bitmap);
imageView.setMaxHeight(50);
imageView.setMaxWidth(50);
ll.addView(imageView);
运行之后,Imageview 仍然被撑开,难道是MaxWidth,MaxHeight不起作用,抓紧翻阅api文档,找到下面的解释:
An optional argument to supply a maximum width for this view. Only valid if setAdjustViewBounds(boolean) has been set to true.
文档中说得很清楚,抓紧修改如下:
再次运行,竟然可以了!LinearLayout ll = new LinearLayout(ConsumeInfo.this);
ll.setOrientation(LinearLayout.HORIZONTAL);
//设置小图标
ImageView imageView = new ImageView(ConsumeInfo.this);
Bitmap bitmap = BitmapFactory.decodeFile((String)mData.get(position).get("imgpath"));
imageView.setImageBitmap(bitmap);
imageView.setAdjustViewBounds(true);
imageView.setMaxHeight(50);
imageView.setMaxWidth(50);
ll.addView(imageView);
在Android开发中,遇到ImageView设置MaxWidth和MaxHeight后仍被图片撑开的问题。通过查阅API文档发现,只有当设置ImageView的setAdjustViewBounds为true时,MaxWidth和MaxHeight才会生效。解决方案是在设置图片后,添加imageView.setAdjustViewBounds(true)来限制图片的最大尺寸。
1611

被折叠的 条评论
为什么被折叠?



