2015-09-26 65 views
0

返回零我知道這是一種重複,但其他問題的答案沒有幫助我。總是我的ImageView高度返回零。這裏是我的嘗試:ImageView高度始終在android

dart.getViewTreeObserver().addOnPreDrawListener(
    new ViewTreeObserver.OnPreDrawListener() { 
     public boolean onPreDraw() { 
     int finalHeight = dart.getMeasuredHeight(); 
     int finalWidth = dart.getMeasuredWidth(); 
     Toast.makeText(MainActivity.this," "+finalHeight,Toast.LENGTH_SHORT).show(); 
     return true; 
    } 
}); 

而且 -

@Override 
public void onWindowFocusChanged(boolean hasFocus) { 
    super.onWindowFocusChanged(hasFocus); 
    height = dart.getHeight(); 
    width = dart.getWidth(); 
    Toast.makeText(MainActivity.this," "+height,Toast.LENGTH_SHORT).show(); 
} 

而且我也試過dart.getHeight()onCreate內,但我的理解是不能工作。我能做什麼?

回答

0

添加addonPreDrawListenerViewTreeObserver,並在您onPreDraw()方法

ViewTreeObserver vto = iv.getViewTreeObserver(); 
    vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { 
     public boolean onPreDraw() { 
      dart.getViewTreeObserver().removeOnPreDrawListener(this); 
      finalHeight = dart.getMeasuredHeight(); 
      finalWidth = dart.getMeasuredWidth(); 
      Toast.makeText(MainActivity.this," "+finalHeight,Toast.LENGTH_SHORT).show(); 
      return true; 
     } 
    }); 
+0

它顯示正確的高度,但我想在我的onCreate方法來使用它撥打dart.getViewTreeObserver().removeOnPreDrawListener(this);,能不能做到?如果我在onPreDraw()之外使用它,它仍然爲零.. – DavidBalas2

+0

好吧我在onPreDraw():)中使用它 – DavidBalas2