You cannot use the width
/height
/getMeasuredWidth
/getMeasuredHeight
on a View
before the system renders it (typically from onCreate
/onResume
).
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();
...
}
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…