在使用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 ifsetAdjustViewBounds(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);

再次运行,竟然可以了!

更多相关文章

  1. ImageButton与Button
  2. Android(安卓)EditText文本输入框禁止输入空格
  3. Android:UI控件风格与主题、selector、Theme
  4. Android(安卓)中文 API 文档 (44) —— Chronometer.OnChronometer
  5. android 获取坐标[整理]
  6. 【Android】重写原生控件及使用
  7. Android官方文档翻译 十 2.3Styling the Action Bar
  8. Android(安卓)点击事件冲突之拦截父控件事件 交给子控件处理
  9. Android自定义进度条-带文本(文字进度)的水平进度条(ProgressBar

随机推荐

  1. android 通过eclipse混淆代码 打包 + pro
  2. Android获取sd卡上的文件目录-日记
  3. android 加载图片防止内存溢出
  4. 实现 Android(安卓)应用在开机时自启动
  5. Android——自定义拦截器实现OKHttp网络
  6. android 反编译必知-public.xml
  7. Android单例模式
  8. Android获得文件夹大小
  9. 继承FlexboxLayout自定义可自动换行的tag
  10. android 工具类