2
這些方法在例如爲動態檢索時返回正確的值。在onCreate()中,但唯一的例外是當尺寸在xml佈局文件中被指定爲undefined時, 就像'包裝內容'等。有沒有辦法檢索這樣的值?Android layoutParams getWidth()getHeight()
這些方法在例如爲動態檢索時返回正確的值。在onCreate()中,但唯一的例外是當尺寸在xml佈局文件中被指定爲undefined時, 就像'包裝內容'等。有沒有辦法檢索這樣的值?Android layoutParams getWidth()getHeight()
這應該返回正確的寬度和高度。在子視圖佈局完成之前調用onCreate。所以寬度和高度尚未計算。獲取高度和寬度。把這個onCreate方法
LinearLayout layout = (LinearLayout)findViewById(R.id.YOUD VIEW ID);
ViewTreeObserver vto = layout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
hs.getViewTreeObserver().removeGlobalOnLayoutListener(this);
int width = layout.getMeasuredWidth();
int height = layout.getMeasuredHeight();
}
});
什麼是** hs **? – hakiko
@hakiko我認爲它應該是layout.getViewTreeObserver()。removeGlobalOnLayoutListener(this); – blessenm
或者如果你添加'final'到'vto',那麼你可以在'onGlobalLayout()'內使用'vto.removeGlobalOnLayoutListener(this)'' – vilpe89