2013-06-19 174 views
0

它是我的代碼。 referButtun是的ImageButton爲什麼view.getHeigth在onResume()上返回0?

@Override 
protected void onResume() { 
    super.onResume(); 
    referButtun.getHeight(); 
    Log.d("Base",""+referButtun.getHeight()); 
} 
+1

有許多重複的問題,併爲這個答案,例如http://stackoverflow.com/q/4393612/534471 –

回答

0

這就是你應該怎麼做:

final ViewTreeObserver vto = referButtun.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() { 
     Log.d("Base",""+referButtun.getHeight()); 

     // don't forget to remove the listener, we don't need it just this one time 
     vto.removeGlobalOnLayoutListener(this); 
    } 
}); 

作爲替代,你可以使用

referButtun.post(new Runnable() { 
    @Override 
    public void run() { 
     Log.d("Base",""+referButtun.getHeight()); 
    } 
});