问题描述
如果我在onResume调用getMeasuredWidth()或的getWidth()进行布局,他们将返回0。我认为,鉴于它尚未领取的在这一刻。
If I call getMeasuredWidth() or getWidth() for layout in onResume they returns 0.I think that view it's not drawn yet in this moment.
此外,我认为我需要把getMeasuredWidth()或的getWidth()在一个名为布局绘制和查看测量被称为后回调方法。我应该用什么Android的回调方法?
Also I think that I need to put getMeasuredWidth() or getWidth() in callback method called after layout is drawn and view measurements are known.What android callback method should I use?
推荐答案
您不能使用宽/高/ getMeasuredWidth / getMeasuredHeight一个查看
前的系统使得它(通常的onCreate / onResume)。
You cannot use the width/height/getMeasuredWidth/getMeasuredHeight on a View
before the system renders it (typically from onCreate/onResume).
这个简单的解决方法是发布的Runnable
的布局。可运行将在查看
已经奠定了之后执行。
Simple solution for this is to post a Runnable
to the layout. The runnable will be executed after the View
has been laid out.
BoxesLayout = (RelativeLayout) findViewById(R.id.BoxesLinearLayout);
BoxesLayout.post(new Runnable() {
@Override
public void run() {
int w = BoxesLayout.getMeasuredWidth();
int h = BoxesLayout.getMeasuredHeight();
...
}
});
这篇关于如果我在onResume调用getMeasuredWidth()或的getWidth()进行布局则返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!