笔记85--onCreate中获取控件宽和高
来源:互联网 发布:淘宝宠物狗怎么快递 编辑:程序博客网 时间:2024/05/21 07:55
已找不到原作者。
常识:在onCreate()中获取的控件宽和高是0。因为onCreate()执行完了,我们定义的控件才会被度量。
方法一: 比其他的两个方法多了一次计算,也就是多调用了一次onMeasure()方法,该方法虽然看上去简单,但是如果要目标控件计算耗时比较大的话,不见时使用,如listView等.
方法二,它的回调方法会调用很多次,并且滑动TextView的时候任然会调用,所以不建议使用.
建议使用方法三.
解决方法:
方法1:
int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED); int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED); imageView.measure(w, h); int height =imageView.getMeasuredHeight(); int width =imageView.getMeasuredWidth(); textView.append("\n"+height+","+width);
方法2:
ViewTreeObserver vto = imageView.getViewTreeObserver(); vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { public boolean onPreDraw() { int height = imageView.getMeasuredHeight(); int width = imageView.getMeasuredWidth(); textView.append("\n"+height+","+width); return true; } });
方法3:
ViewTreeObserver vto2 = imageView.getViewTreeObserver(); vto2.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { imageView.getViewTreeObserver().removeGlobalOnLayoutListener(this); textView.append("\n\n"+imageView.getHeight()+","+imageView.getWidth()); } });
0 0
- 笔记85--onCreate中获取控件宽和高
- Android在View onCreate时获取控件的宽和高
- Android中获取控件的宽和高
- Android oncreate中获取控件宽高
- [Android]在onCreate方法中中获取View的宽和高
- 获取控件的宽和高
- 获取控件的宽和高
- 获取控件的宽和高
- android获取控件宽和高
- android 在Activity的OnCreate()方法中如何获取view的宽和高
- fragment中获取控件的宽和高的方法
- android 在onCreate中获取控件的宽高
- Android 获取控件的宽和高
- 获取Android控件的宽和高
- 获取Android控件的宽和高
- 获取Android控件的宽和高
- 获取Android控件的宽和高
- 获取Android控件的宽和高
- Ubuntu14.04下手工搭lamp及本项目的一些插件的过程
- Storm 入门与安装
- Hadoop读书笔记(八)MapReduce 打成jar包demo
- ubuntu14.04主题安装
- (四)多例模式
- 笔记85--onCreate中获取控件宽和高
- css的vertical-align属性
- android sdk manager 更新慢解决办法 转自http://blog.csdn.net/exlsunshine/article/details/22208857
- 系统架构试题回顾
- Python 第一章 基础知识(8) 保存并执行程序
- How crusher for use and maintenance?
- Nginx配置proxy_pass转发的/路径问题
- 阴影介绍 the introduction of shadow
- C语言函数调用栈剖析