2016-02-01 113 views
1

我想獲取設置爲包裝內容的線性佈局的高度。但它是零。 我試試這個代碼:獲取設置爲包裝內容的佈局的高度

final LinearLayout below= (LinearLayout) findViewById(R.id.below); 


     ViewTreeObserver vto = below.getViewTreeObserver(); 
     vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
      @Override 
      public void onGlobalLayout() { 
        width = below.getMeasuredWidth(); 
        height = below.getMeasuredHeight(); 

      } 
     }); 

和:

LinearLayout below= (LinearLayout) findViewById(R.id.below); 
     final int belowh=below.getHeight(); 

使用代碼:

final LinearLayout below= (LinearLayout) findViewById(R.id.below); 
     ViewTreeObserver vto = below.getViewTreeObserver(); 
     vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
      @Override 
      public void onGlobalLayout() { 

     if(below.getMeasuredHeight()> 0){ 

       this.below.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
       int width = below.getMeasuredWidth(); 
       int height = below.getMeasuredHeight(); 
     } 
      } 
     }) 

    ; 

click here to see

+0

你可以參考這一次鏈接[拿到佈局的高度已經WRAP_CONTENT財產(http://stackoverflow.com/questions/13062401/get-the- height-of-layout-which-have-wrap-content-property) –

+0

你應該在視圖佈局後調用它... –

+0

@ Md.ShahadatSarker感謝它的正常工作。 –

回答

0

試試這個

final LinearLayout below= (LinearLayout) findViewById(R.id.below); 
    ViewTreeObserver vto = below.getViewTreeObserver(); 
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
     @Override 
     public void onGlobalLayout() { 

    if(below.getMeasuredHeight()> 0){ 

      this.below.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
      int width = below.getMeasuredWidth(); 
      int height = below.getMeasuredHeight(); 
    } 
     } 
    }) 

; 
+0

我以前試過,但我不知道爲什麼下面的代碼不能在這一行中解決:this.below.getViewTreeObserver()。removeGlobalOnLayoutListener(this); –

+0

我已經明白了我的答案,請現在檢查。 –

+0

我再次添加你的代碼,再次出現這個問題。你可以看到我在這個問題上的意思。我編輯它添加它的圖片。點擊該鏈接,看看有什麼問題。 –

3

試試這個代碼用於獲取佈局的寬度和高度後,您的觀點是已創建

final LinearLayout below= (LinearLayout) findViewById(R.id.below); 
    below.post(new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       Log.i("TAG", "Layout width :"+ below.getWidth()); 
       Log.i("TAG", "Layout height :"+ below.getHeight()); 
      } 
    }); 

希望這有助於

+0

你的代碼效果很好,也@Md。 Shahadat Sarker癱瘓工程 - 在視圖佈置後稱之爲 - 但我不知道哪一個更好。 –

0

@coder安卓

如果你仍然在此堆疊....請參閱本。我認爲這會很好地工作。

下面的代碼被發現How to retrieve the dimensions of a view?

final TextView tv = (TextView)findViewById(R.id.image_test); 
ViewTreeObserver vto = tv.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 

    @Override 
    public void onGlobalLayout() { 
     LayerDrawable ld = (LayerDrawable)tv.getBackground(); 
     ld.setLayerInset(1, 0, tv.getHeight()/2, 0, 0); 
     ViewTreeObserver obs = tv.getViewTreeObserver(); 

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { 
      obs.removeOnGlobalLayoutListener(this); 
     } else { 
      obs.removeGlobalOnLayoutListener(this); 
     } 
    } 

});