如果我在 onResume 中调用 getMeasuredWidth() 或 getWidth() 进行布局,它们将返回 0。
我认为此时此刻还没有绘制出该视图。
另外,我认为我需要将 getMeasuredWidth() 或 getWidth() 放入绘制布局并且视图测量已知后调用的回调方法中。
我应该使用什么android回调方法?
您不能使用width
/height
/getMeasuredWidth
/getMeasuredHeight
on a View
在系统渲染它之前(通常来自onCreate
/onResume
).
简单的解决方案是发布一个Runnable
到布局。该可运行程序将在以下之后执行View
已被布置。
BoxesLayout = (RelativeLayout) findViewById(R.id.BoxesLinearLayout);
BoxesLayout.post(new Runnable() {
@Override
public void run() {
int w = BoxesLayout.getMeasuredWidth();
int h = BoxesLayout.getMeasuredHeight();
...
}
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)