2014-07-12 46 views
1

的高度和寬度在我的應用程序具有低於性質如何應在運行時獲得的佈局正確的高度,我將裏面的佈置很多意見動態獲取佈局

 LinearLayout ll = new LinearLayout(this); 
     ll.setLayoutParams(new LayoutParams(840,LayoutParams.WRAP_CONTENT)); 
     ll.setOrientation(LinearLayout.VERTICAL); 

     ll.addView(view1); 
     ll.addView(view2); 

     ll.getHeight(); 

如何得到的高度那麼佈局考慮添加view1和view2,而不考慮我的屬性(LayoutParams.WRAP_CONTENT),因爲它總是返回0給我?

+0

可能是[這](http://stackoverflow.com/questions/12068945/get-layout-height-and-width-at-run-time-android)是你想要 –

+0

getMeasuredHeight什麼( )和getMeasuredWidth() – kgandroid

+0

嘿你有沒有做到這一點或不是仍然? –

回答

1

您可以將孩子的意見後稱

ll.post(new Runnable() 
{ 
    public void run() 
    { 
     ll.getHeight(); 
    } 
}); 

這會給你ll的高度。

+0

如何在UI線程中獲得這個高度? – Strawberry

+0

對不起,我不知道。 – Apoorv

2

試試這種方式,希望這會幫助你解決你的問題。

int width; 
int height; 

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

    } 
});