我使用我的根布局做到了这一点,问题是当它们第一次运行时,您无法在 onCreate/onStart/onResume 方法中获取此信息(宽度和高度)。
在那之后的任何时候,都可以从你的根布局(LinearLayout/FrameLayout/TableLayout/etc)中检索大小,在我的例子中,我使用的是 FrameLayout:
FrameLayout f = (FrameLayout) findViewById(R.id.layout_root);
Log.d(TAG, "width " + f.getMeasuredWidth() );
Log.d(TAG, "height " + f.getMeasuredHeight() );
或:
FrameLayout f = (FrameLayout) findViewById(R.id.layout_root);
Log.d(TAG, "width " + f.getRight() );
Log.d(TAG, "height " + f.getBottom() );
从这里编辑 -------------------------------------------- ------------
我找到了一种在 onCreted 方法上获取此信息的方法,但前提是您有多个活动。就我而言,我有一个称为第二个活动的主要活动。
在调用 second_activity 之前,在 main_activity 上,通过一个新的 Itent,我可以为 second_activity 添加额外的信息。这是您在第一个活动中需要的代码:
Intent i = new Intent(this, SecondActivity.class);
i.putExtra("width", f.getMeasuredWidth());
i.putExtra("height", f.getMeasuredHeight());
startActivity(i);
在您的第二个活动之后,您可以在第二个活动的 onCreate 方法上检索此信息:
int width = getIntent().getExtras().getInt("width");
int height = getIntent().getExtras().getInt("height");