在activity中可以调用View.getWidth、View.getHeight()、View.getMeasuredWidth() 、View.getgetMeasuredHeight()来获得某个view的宽度或高度,但是在onCreate()、onStrart()、onResume()方法中会返回0,这是应为当前activity所代表的界面还没显示出来没有添加到WindowPhone的DecorView上或要获取的view没有被添加到DecorView上或者该View的visibility属性为gone 或者该view的width或height真的为0 ,所以只有上述条件都不成立时才能得到非0的width和height。

View的事件回调里使用

这时候该view已经被显示即被添加到DecorView上 如点击事件 触摸事件 焦点事件等

View view=findViewById(R.id.tv);      view.setOnClickListener(new OnClickListener() {                    @Override          public void onClick(View v) {              int width = v.getWidth();          }      });

onWindowFocusChanged() 回调

在activity被显示出来时即添加到了DecorView上时获取宽和高如onWindowFocusChanged() 回调方法

@Override  public void onWindowFocusChanged(boolean hasFocus) {      View iv1 = findViewById(R.id.iv1);      View iv2=findViewById(R.id.iv2);      String msg1="iv1' width:"+iv1.getWidth()+" height:"+iv1.getHeight()+"      measuredWidth:"+iv1.getMeasuredWidth()+"measuredHeight:"+iv1.getMeasuredHeight();      String msg2="iv2' width:"+iv2.getWidth()+" height:"+iv2.getHeight()+"      measuredWidth:"+iv2.getMeasuredWidth()+"measuredHeight:"+iv2.getMeasuredHeight();      Log.i("onWindowFocusChanged() "+msg1);      Log.i("onWindowFocusChanged() "+msg2);      super.onWindowFocusChanged(hasFocus);  }

延时

在onResume方法最后开线程300毫秒左右后获取宽和高 因为onResume执行完后300毫秒后,界面就显示出来了。

view.postDelayed(new Runnable() {                            @Override              public void run() {                  View iv1 = findViewById(R.id.iv1);                  View iv2=findViewById(R.id.iv2);                  String msg1="iv1' width:"+iv1.getWidth()+" height:"+iv1.getHeight()+"  measuredWidth:"+iv1.getMeasuredWidth()+"measuredHeight:"+iv1.getMeasuredHeight();                  String msg2="iv2' width:"+iv2.getWidth()+" height:"+iv2.getHeight()+"  measuredWidth:"+iv2.getMeasuredWidth()+"measuredHeight:"+iv2.getMeasuredHeight();                  Log.i("onWindowFocusChanged() "+msg1);                  Log.i("onWindowFocusChanged() "+msg2);              }          }, 300);

在onCreate()或onResume()等方法中需要

getViewTreeObserver().addOnGlobalLayoutListener()来添为view加回调在回调里获得宽度或者高度获取完后让view删除该回调

view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {                    @Override          public void onGlobalLayout() {              view.postDelayed(new Runnable() {                                    @Override                  public void run() {                      View iv1 = findViewById(R.id.iv1);                      View iv2=findViewById(R.id.iv2);                      String msg1="iv1' width:"+iv1.getWidth()+" height:"+iv1.getHeight()+"  measuredWidth:"+iv1.getMeasuredWidth()+"measuredHeight:"+iv1.getMeasuredHeight();                      String msg2="iv2' width:"+iv2.getWidth()+" height:"+iv2.getHeight()+"  measuredWidth:"+iv2.getMeasuredWidth()+"measuredHeight:"+iv2.getMeasuredHeight();                      Log.i("onWindowFocusChanged() "+msg1);                      Log.i("onWindowFocusChanged() "+msg2);                  }              }, 300);          }      });

我是天王盖地虎的分割线

参考:http://blog.csdn.net/nailsoul/article/details/25909313

更多相关文章

  1. Android自定义控件 之 轮播图(ViewPager)重点及学习
  2. Android模拟器获取IP的方法
  3. Android去掉标题栏点菜单键程序崩溃
  4. android gallery3d 源码分析(二)
  5. Android获取全局Context的方法
  6. Android(安卓)输入系统(三)InputReader
  7. Android保持屏幕常亮唤醒状态的方法
  8. Android(安卓)自定义左滑删除列表
  9. Android中打电话的数据流程分析

随机推荐

  1. 《PHP环境搭建-基础语法及数据类型》
  2. PHP运算符、分支及循环
  3. php入门知识:运算符&分支与循环结构
  4. python 3.8.6初学笔记
  5. 0108-JSON二个函数,get,post发起ajax请求
  6. php运算符实例|分支与循环结构|替代语法|
  7. PHP基础:运算符,分支结构,循环以及文件包
  8. 新的一年对自己的希望
  9. PHP基础知识:常见运算符、语句分支(模板写
  10. php流程控制和循环结构