android获取view宽高的几种方法 - ysl_longer
来源:互联网 发布:苹果mac更新系统版本 编辑:程序博客网 时间:2024/05/21 05:19
android获取view宽高的几种方法 - ysl_longer
在onCreate方法中我们通过mView.getWidth()和mView.getHeight()获取到的view的宽高都是0,那么下面几种方法就可以在onCreate方法中获取到view的宽高。
1、
int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); mTextView.measure(w, h); int height = mTextView.getMeasuredHeight(); int width = mTextView.getMeasuredWidth(); System.out.println("measure width=" + width + " height=" + height);
2、mViewTreeObserver = mTextView.getViewTreeObserver();
mViewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { // TODO Auto-generated method stub mTextView.getViewTreeObserver().removeGlobalOnLayoutListener(this); System.out.println("onGlobalLayout width=" + mTextView.getWidth() + " height=" + mTextView.getHeight()); } });
3、
mViewTreeObserver.addOnPreDrawListener(new OnPreDrawListener() { @Override public boolean onPreDraw() { // TODO Auto-generated method stub mTextView.getViewTreeObserver().removeOnPreDrawListener(this); System.out.println("onPreDraw width=" + mTextView.getWidth() + " height=" + mTextView.getHeight()); return true; } });
4、
@Override public void onWindowFocusChanged(boolean hasFocus) { // TODO Auto-generated method stub super.onWindowFocusChanged(hasFocus); if (hasFocus) { System.out.println("onWindowFocusChanged width=" + mTextView.getWidth() + " height=" + mTextView.getHeight()); } }
0 0
- android获取view宽高的几种方法 - ysl_longer
- Android--获取View的宽高的几种方法
- android获取view宽高的几种方法,解决获取高宽为0问题
- Android中获取view的宽高的几种方法
- Android获取View宽高的几种方式
- Android获取View宽高的几种方式
- Android获取View宽高的几种方式
- Android获取View宽高的几种方式
- Android 获取view的宽高方法
- android 开发中获取View视图的宽和高的几种方法
- android 开发中获取View视图的宽和高的几种方法
- Android 开发中获取View视图的宽和高的几种方法
- Android 几种获得view的宽和高的方法
- 获取View 宽高的几种方式
- onCreate中获取view宽高的几种方式
- Android在onCreate方法中获取view的宽高
- android中获取View宽高的方法
- Android开发显示之前获取view的宽高方法
- UVa 11472 - Beautiful Numbers
- OpenGL ES2.0开发经验(一) :
- 集合初探(一)
- matlab中 bsxfun函数
- 直接初始化与拷贝(复制)初始化
- android获取view宽高的几种方法 - ysl_longer
- 当你输入一个网址,实际会发生什么?
- CAS单点登录python客户端分析
- 文字动画(Textdh)
- 不要为做而做
- java学习心得 关于如何设计一个拼图游戏
- ActiveAndroid 简单方便的ORM(对象关系映射)
- web.xml配置
- 需要看的书籍