zoukankan      html  css  js  c++  java
  • Activity中获取view的高度和宽度为0的原因以及解决方案

    在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  

    所以要想获取到的width和height为真实有效的 则有以下方法

    1.在该View的事件回调里使用  这时候该view已经被显示即被添加到DecorView上  如点击事件  触摸事件   焦点事件等

    1 View view=findViewById(R.id.tv);  
    2     view.setOnClickListener(new OnClickListener() {  
    3           
    4         @Override  
    5         public void onClick(View v) {  
    6             int width = v.getWidth();  
    7         }  
    8     });  

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

     
     1 @Override  
     2   public void onWindowFocusChanged(boolean hasFocus) {  
     3     View iv1 = findViewById(R.id.iv1);  
     4 View iv2=findViewById(R.id.iv2);  
     5 String msg1="iv1' "+iv1.getWidth()+" height:"+iv1.getHeight()+"  measuredWidth:"+iv1.getMeasuredWidth()+"measuredHeight:"+iv1.getMeasuredHeight();  
     6 String msg2="iv2' "+iv2.getWidth()+" height:"+iv2.getHeight()+"  measuredWidth:"+iv2.getMeasuredWidth()+"measuredHeight:"+iv2.getMeasuredHeight();  
     7 i("onWindowFocusChanged() "+msg1);  
     8 i("onWindowFocusChanged() "+msg2);  
     9     super.onWindowFocusChanged(hasFocus);  
    10   }  

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

    当然地2种和地3种方法要保证获取宽高的view是在setContentView时设进去的View或它的子View

     1 view.postDelayed(new Runnable() {  
     2               
     3             @Override  
     4             public void run() {  
     5                 View iv1 = findViewById(R.id.iv1);  
     6                 View iv2=findViewById(R.id.iv2);  
     7                 String msg1="iv1' "+iv1.getWidth()+" height:"+iv1.getHeight()+"  measuredWidth:"+iv1.getMeasuredWidth()+"measuredHeight:"+iv1.getMeasuredHeight();  
     8                 String msg2="iv2' "+iv2.getWidth()+" height:"+iv2.getHeight()+"  measuredWidth:"+iv2.getMeasuredWidth()+"measuredHeight:"+iv2.getMeasuredHeight();  
     9                 i("onWindowFocusChanged() "+msg1);  
    10                 i("onWindowFocusChanged() "+msg2);  
    11             }  
    12         }, 300);  

    4.在onCreate()或onResume()等方法中需要获取宽高时使用getViewTreeObserver().addOnGlobalLayoutListener()来添为view加回调在回调里获得宽度或者高度获取完后让view删除该回调

     1 view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {  
     2           
     3         @Override  
     4         public void onGlobalLayout() {  
     5             view.postDelayed(new Runnable() {  
     6                   
     7                 @Override  
     8                 public void run() {  
     9                     View iv1 = findViewById(R.id.iv1);  
    10                     View iv2=findViewById(R.id.iv2);  
    11                     String msg1="iv1' "+iv1.getWidth()+" height:"+iv1.getHeight()+"  measuredWidth:"+iv1.getMeasuredWidth()+"measuredHeight:"+iv1.getMeasuredHeight();  
    12                     String msg2="iv2' "+iv2.getWidth()+" height:"+iv2.getHeight()+"  measuredWidth:"+iv2.getMeasuredWidth()+"measuredHeight:"+iv2.getMeasuredHeight();  
    13                     i("onWindowFocusChanged() "+msg1);  
    14                     i("onWindowFocusChanged() "+msg2);  
    15                 }  
    16             }, 300);  
    17         }  
    18     });  

    5.在窗口焦点发生变化时获取宽高 onResume完后就会把界面渲染到窗口上 渲染完后将执行窗口焦点花生变化回调   所以onResume到 onWindowFocusChanged为把界面渲染到窗口的时间

     1 boolean measure;  
     2     View iv1;  
     3     View iv2;  
     4     @Override  
     5     public void onWindowFocusChanged(boolean hasFocus) {  
     6         super.onWindowFocusChanged(hasFocus);  
     7         if(hasFocus && !measure){  
     8             measure=true;  
     9             measure();  
    10         }  
    11     }  
    12     private void findViews(){  
    13         iv1 = findViewById(R.id.iv1);  
    14         iv2 =findViewById(R.id.iv2);  
    15     }  
    16     private void measure(){  
    17         String msg1="iv1' "+iv1.getWidth()+" height:"+iv1.getHeight()+"  measuredWidth:"+iv1.getMeasuredWidth()+"measuredHeight:"+iv1.getMeasuredHeight();  
    18         String msg2="iv2' "+iv2.getWidth()+" height:"+iv2.getHeight()+"  measuredWidth:"+iv2.getMeasuredWidth()+"measuredHeight:"+iv2.getMeasuredHeight();  
    19         i("onWindowFocusChanged() "+msg1);  
    20         i("onWindowFocusChanged() "+msg2);  
    21     }  
  • 相关阅读:
    循环控制结构程序06 零基础入门学习C语言21
    数组01 零基础入门学习C语言23
    循环控制结构程序07 零基础入门学习C语言22
    数组02 零基础入门学习C语言24
    循环控制结构程序07 零基础入门学习C语言22
    基于VC++2012在Windows8上实现文件隐藏
    实现诺基亚 lumia Windows phone 的手机通话记录截取
    基于Windows8与Visual Studio2012实现杀毒通用模块
    用Visual studio2012在Windows8上开发内核驱动监视进程创建
    ISV客户博客系列:Linx发布它的Windows Azure销售点系统
  • 原文地址:https://www.cnblogs.com/zl1991/p/5340199.html
Copyright © 2011-2022 走看看