我想獲取設置爲包裝內容的線性佈局的高度。但它是零。 我試試這個代碼:獲取設置爲包裝內容的佈局的高度
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();
}
}
})
;
你可以參考這一次鏈接[拿到佈局的高度已經WRAP_CONTENT財產(http://stackoverflow.com/questions/13062401/get-the- height-of-layout-which-have-wrap-content-property) –
你應該在視圖佈局後調用它... –
@ Md.ShahadatSarker感謝它的正常工作。 –