我知道在设置 UI 之前我不能过早地获得尺寸。
我知道我必须在 onCreate() 之后获取尺寸。
但即使我执行以下操作,他们仍然总是返回 0。
创建():
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_path);
...
onCreateDone = true;
}
我希望在 onSensorChanged() 中获得尺寸
// called when sensor values change
public void onSensorChanged(SensorEvent event) { // is roughly called 350 times in 1s
if (layoutSizeNotObtained && onCreateDone) { // only runs once to get the layout size
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.activity_show_path_relativelayout);
int layoutWidth = relativeLayout.getWidth();
int layoutHeight = relativeLayout.getHeight();
Log.d("ShowPathActivity", "layout size: " + layoutWidth + " " + layoutHeight);
Constant.setInitialX(layoutWidth / 2);
Constant.setInitialY(layoutHeight / 2);
layoutSizeNotObtained = false;
}
....
}
哪里出错了?