简体   繁体   English

方向改变时如何获取屏幕尺寸? [android]

[英]How do i get the dimensions of the screen when orientation changes? [android]

I know how to get the dimensions of the screen onCreate. 我知道如何在onCreate上获取屏幕的尺寸。 I was making a runnable which will allow me to get the dimensions inside of that. 我正在制作一个可运行的设备,这将使我能够获得其中的尺寸。

The thing is that when i rotate my device, it doesnt seem to properly return the dimensions. 问题是,当我旋转设备时,它似乎无法正确返回尺寸。

I was thinking to do it onConfigurationChanged() as that seems to firre when i rotate my device. 我当时想在onConfigurationChanged()上执行此操作,因为旋转设备时似乎会触发。 I was trying to do the following but i dont think it was giving valid data. 我正在尝试执行以下操作,但我不认为它提供了有效的数据。

Rect r = new Rect();
Window win = getWindow();
win,getDecorView().getWindowVisibleDisplayFrame(r);
int height = r.height();
int width = r.width();

but it seems that they arent correct. 但似乎他们没有正确。 I was trying to move a created item around the screen, and it doesnt go to the edges of the screen. 我试图在屏幕上移动创建的项目,但它没有到达屏幕的边缘。 I dont think it has anything to do with my drag function. 我不认为这与我的拖动功能有关。

I feel the dimensions are incorrect. 我觉得尺寸不正确。

How is this done? 怎么做? Am i on the right path? 我在正确的道路上吗?

The workaround i had created was that onConfigurationChange() I created a bundle with attributes and then recalled onCreate(). 我创建的解决方法是onConfigurationChange()创建了具有属性的包,然后调用了onCreate()。 Essentially this would recreate in the new orientation. 本质上,这将以新的方向重新创建。

This was what i did to get around this issue at hand, and it made it so the layout is rerendered. 这是我为解决此问题而做的,并且做到了,因此重新渲染了布局。

Bundle b = new Bundle();
b.putString("test","data here");
onCreate(b);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM