简体   繁体   English

是否可以知道何时更改了屏幕方向,但在清单上强加了肖像模式?

[英]It is possible to known when the screen orientation is changed but with portrait mode forced on manifest?

Welcome all 欢迎大家

I have a special need for my app. 我对我的应用有特殊需要。 I must force the app to work only in portrait mode, but i need to know when the user has moved the phone to landscape mode. 我必须强制该应用程序仅在纵向模式下工作,但是我需要知道用户何时将手机移至横向模式。 Why? 为什么? because i am displaying a opengl view with a texture image, and when the user changues the phone position to landscape mode i must rotate the polygon without reseting the activity. 因为我正在显示带有纹理图像的opengl视图,并且当用户将手机位置更改为横向模式时,我必须旋转多边形而不重置活动。 Then i must force portrait mode on manifest, because i dont want that my onCreate method gets called again. 然后,我必须在清单上强制使用肖像模式,因为我不希望再次调用我的onCreate方法。

Please, can someone tell me how to achieve this? 拜托,有人可以告诉我如何实现这一目标吗?

I know how to rotate the image, i only need to know when the user has moved the phone to landscape position, but with portrait forced on manifest. 我知道如何旋转图像,我只需要知道用户何时将手机移动到横向位置,但必须将肖像强制显示在清单上。

I tryed adding android:screenOrientation="portrait" in the manifest and also adding android:configChanges="keyboardHidden|orientation" to the declaration in the manifest; 我尝试在清单中添加android:screenOrientation =“ portrait”,也将android:configChanges =“ keyboardHidden | orientation”添加到清单中的声明中; and then overriding onConfigurationChanged() in my activity. 然后在我的活动中覆盖onConfigurationChanged()。 But this doens't works, because portrait mode is forzed, then onConfigurationChanged method is never called...... 但这是行不通的,因为伪造了肖像模式,所以永远不会调用onConfigurationChanged方法……

Thanks 谢谢

Why not use the SensorManager to monitor when the phone has rotated 90 degrees. 为什么不使用SensorManager监视手机旋转90度的时间。 Actually this may be helpful also. 实际上, 也可能会有所帮助。

use this attribute in manifest android:configChanges="orientation" this stops recreating activity then override the onConfigurationChange() method. 在清单android:configChanges =“ orientation”中使用此属性,这将停止重新创建活动,然后覆盖onConfigurationChange()方法。 Give the same layout both in portrait and landscape mode. 在纵向和横向模式下都提供相同的布局。 when orientation change occurs, that method wil be called then change the image as u like 当方向发生变化时,将调用该方法,然后像您一样更改图像

Do Not set the screen orientation in the manifest (android:screenOrientation="portrait"). 不要在清单中设置屏幕方向(android:screenOrientation =“ portrait”)。 Add the android:configChanges="keyboardHidden|orientation", and override your onConfigurationChanged(). 添加android:configChanges =“ keyboardHidden | orientation”,并覆盖您的onConfigurationChanged()。 This way your onCreate will not be called twice. 这样,您的onCreate不会被调用两次。

Here a example of your onConfigurationChanged: 以下是onConfigurationChanged的示例:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig); // tem que ter
    if (getResources().getConfiguration().orientation ==  Configuration.ORIENTATION_LANDSCAPE) {

    } else 
    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {

    }
}

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

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