`
寻梦者
  • 浏览: 639980 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Android ImageView控件的MaxWidth、MaxHeight不起作用

 
阅读更多

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

 

再次运行,竟然可以了!     

分享到:
评论
1 楼 家有吃貨 2013-04-27  
谢谢哈  刚好犯同样错误了

相关推荐

    Android应用开发-ImageView控件.pptx

    当设置为`true`时,ImageView会根据图片的长宽比调整自己的尺寸,但请注意,这个属性需要配合`android:maxHeight`和`android:maxWidth`属性一起使用,才能限制ImageView的最大尺寸,防止图片过大导致布局问题。...

    Android2.2 API 中文文档系列(6) —— ImageView

    当设置为 `true` 时,`ImageView` 会根据 `maxWidth` 和 `maxHeight` 的限制自动调整自身的宽高比,确保图像不失真。如果只设置了一个值(`maxWidth` 或 `maxHeight`),那么另一个值将会被自动调整以保持原始的比例...

    android常用控件属性方法事件说明.doc

    `maxHeight`和`maxWidth`限制了图片的最大高度和宽度;`scaleType`决定了图片如何适应ImageView的大小,有fitXY、centerCrop等多种模式;`src`属性设置要显示的图像资源,而`background`则用于设置背景图像。 以上...

    android学习之展示图片资源

    - `android:maxHeight` 和 `android:maxWidth`:限制`ImageView`的最大高度和宽度,避免因图片过大而导致界面布局出现问题。 示例代码: ```xml <ImageView android:id="@+id/image" android:layout_width=...

    基础安卓自学汇总(网课学习)

    + android:maxWidth + android:maxHeight + android:adjustViewBounds * 缩放类型: + fitStart + fitCenter + fitEnd + fitXY + center + centerCrop Android 基础知识点小结: * 了解控件的基本属性和...

    android 画面应用

    - `android:maxWidth`:定义 `ImageView` 的最大宽度。 综上所述,`View` 和 `ViewGroup` 以及它们的各种子类共同构成了Android应用程序中用户界面的基础架构。通过合理选择和组合这些控件,开发者可以创建出既...

    控件里面的进度条条Sekbar的用法,下面将用Imageview来具体的演示。

    android:minHeighthuo或(maxHeight)来控制progress的粗细(就是滑动条的粗细) android:layout_width=“match_parent” 整个进度条的宽度 android::layout_height=“wrap_content” 整个进度条的高度 android:...

    安卓开发-ProgressBar 几乎全部的用法.zip

    - `android:max`设置最大值,`android:progress`设置当前进度。 - `android:indeterminateDuration`控制不确定进度动画的时长。 - `android:indeterminateBehavior`定义不确定进度动画的循环行为,如连续、周期性...

Global site tag (gtag.js) - Google Analytics