Tsi*_*mmi 31 android screen screen-size
我怎么知道我的活动的可见大小?
我试图获得活动的实际尺寸,
而不是从getHeight()和的高度和宽度getWidth(),
这给了我屏幕的全尺寸.
Dav*_*ave 40
我想你会想看看Activity.getWindow()和Window.getDecorView(),并获得了宽度/高度.
我使用我的根布局做了那个,问题是当你第一次运行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() );
Run Code Online (Sandbox Code Playgroud)
要么:
FrameLayout f = (FrameLayout) findViewById(R.id.layout_root);
Log.d(TAG, "width " + f.getRight() );
Log.d(TAG, "height " + f.getBottom() );
Run Code Online (Sandbox Code Playgroud)
从这里编辑----------------------------------------------- ---------
我找到了一种在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);
Run Code Online (Sandbox Code Playgroud)
在您的第二个活动之后,您可以在第二个活动的onCreate方法上检索此信息:
int width = getIntent().getExtras().getInt("width");
int height = getIntent().getExtras().getInt("height");
Run Code Online (Sandbox Code Playgroud)
我更喜欢省时的答案:
myActivity.getWindow().getDecorView().getHeight();
myActivity.getWindow().getDecorView().getWidth();
Run Code Online (Sandbox Code Playgroud)