2015-07-20 27 views
0

返回零我使用ViewTreeObserver的OnCreate方法讓我的工具欄和底部佈局的高度,但仍然我得到0高度,爲什麼呢?難道我做錯了什麼?的getHeight()由ViewTreeObserver

這是我特意打電話:

ViewTreeObserver viewTreeObserver = toolbar.getViewTreeObserver(); 
     viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
      @TargetApi(Build.VERSION_CODES.JELLY_BEAN) 
      @Override 
      public void onGlobalLayout() { 
       // Ensure you call it only once : 
       toolbar.getViewTreeObserver().removeOnGlobalLayoutListener(this); 
       height1 = toolbar.getMeasuredHeight(); 
      } 
     }); 

     final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.bottom); 
     ViewTreeObserver vto = linearLayout.getViewTreeObserver(); 
     vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
      @TargetApi(Build.VERSION_CODES.JELLY_BEAN) 
      @Override 
      public void onGlobalLayout() { 
       // Ensure you call it only once : 
       linearLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this); 
       height2 = linearLayout.getMeasuredHeight(); 

      } 
     }); 

     Toast.makeText(getApplicationContext(), String.valueOf(height1) + String.valueOf(height2), Toast.LENGTH_SHORT).show(); 
+0

你嘗試的getWidth()或的getHeight()的處理程序?下面是示例: 新處理程序()postDelayed(新的Runnable(){ \t \t \t @Override \t \t \t公共無效的run(){ \t \t \t \t height1 = toolbar.getMeasuredHeight(); \t \t \t。 \t身高2 = linearLayout.getMeasuredHeight(); \t \t \t} \t \t},1000); –

+0

我也試過這個,但沒有解決方案。 – Spartan

+0

如果佈局的高度爲0,該怎麼辦?你確定佈局是可見的嗎?我問這是因爲我之前在我的項目中錯過了這個。 –

回答

7

看起來你的佈局必須做出多個度量/佈局傳遞和佈局的作品有第一遍後零種尺寸。只有當您有積極的維度時才嘗試刪除OnGlobalLayoutListener。這樣的事情:

if (linearLayout.getMeasuredHeight() > 0) { 
    linearLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this); 
} 
+0

你是什麼意思刪除OnGlobalLayoutListener?我必須直接獲得身高嗎?如果是的話,我沒有任何價值。 – Spartan

+0

@斯巴達看看更新的答案。 – aga

+0

我這樣做,但沒有解決方案:'@Override public void onGlobalLayout(){ if(linearLayout.getMeasuredHeight()> 0){height} = linearLayout.getMeasuredHeight(); linearLayout。getViewTreeObserver()。removeOnGlobalLayoutListener((ViewTreeObserver.OnGlobalLayoutListener)this); } }' – Spartan

0

我遇到了同樣的問題,但理由是不一樣的@aga答案。問題是我的視圖在活動初始​​化時隱藏(在onCreate內部)。在佈局大小計算完成後,我只是移動顯示/隱藏視圖的代碼,以便按預期工作。希望這可以幫助。

0

OnGlobalLayoutListenerViewTreeObserver工作,其方法onGlobalLayout不會立即調用。此偵聽器僅適用於當前佈局中發生的某些事件(當偵聽器顯示某些更改時)。所以,當你加載你的佈局時,你會得到0高度

如果你想 ViewTreeObserver解決外部訪問這個值

是這樣的:

private int height; 

ViewTreeObserver viewTreeObserver = toolbar.getViewTreeObserver(); 
viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() { 
     height = toolbar.getMeasuredHeight(); 
     setHeight(height); 
     // Ensure you call it only once : 
     toolbar.getViewTreeObserver().removeOnGlobalLayoutListener(this); 
    } 
}); 

private void setHeight(int h) { 
    this.height = h; 
    Toast.makeText(getApplicationContext(), String.valueOf(height), Toast.LENGTH_SHORT).show(); 
} 
相關問題