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