如何让ImageView高度和宽度保持一致?
2016-05-20 09:34
627 查看
问题描述
不设置具体数字的高宽,如何让宽高值保持一致?<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/image"/>
也就是说:
竖屏下,height=width;
宽屏下,width=height;
解决方案
扩展ImageView控件,重写ImageView的onMeasure方法int squareDim = 1000000000; @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ super.onMeasure(widthMeasureSpec, heightMeasureSpec); int h = this.getMeasuredHeight(); int w = this.getMeasuredWidth(); int curSquareDim = Math.min(w,h); if(curSquareDim < squareDim) { squareDim = curSquareDim; } Log.d("MyApp", "h "+h+"w "+w+"squareDim "+squareDim); setMeasuredDimension(squareDim, squareDim); }
参考:http://stackoverflow.com/questions/9798392/imageview-have-height-match-width
相关文章推荐
- 关于 java中 求两个字符串中最大的公共子串之引发对return的用法探讨
- 学习SpringMVC(十一)之视图和视图解析器
- POJ 1922 Ride to School (贪心)
- 计算两个日期相差的天数
- Effective Objective-C 2.0 编写高质量iOS与OS X代码 在既有类中使用关联对象存放自定义数据
- 禁止横屏
- Notepad++编写的shell脚本在linux下无法执行的解决方法
- QT历届版本下载总汇
- 一个有趣的基于C++的模拟发牌程序
- 【一天一道LeetCode】#54. Spiral Matrix
- ListView的属性及方法详解
- 【一天一道LeetCode】#54. Spiral Matrix
- 用pyenv和virtualenv搭建单机多版本python虚拟开发环境
- Android:文本过长,实现添加滚动条
- 理解HBase zookeeper和-Root-/.MET表
- 求出今天距离某一天过了多少时间
- WindowManage与Window的在Activity的一点小应用
- 常用正则表达式大全 (转)
- jquery获取当前页面的URL信息
- 切一张图片,如果图片太大,如何解决