我如何禁用景观模式的一些观点在我的Android应用程序?


当前回答

在阅读这篇文章之前,我不知道AndroidManifest.xml文件开关,所以在我的应用程序中,我使用了这个代替:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // Fixed portrait orientation

其他回答

这里的很多答案都建议在AndroidManifest.xml文件中使用“portrait”。这似乎是一个很好的解决方案-但正如文档中所指出的那样,您正在挑出可能只有横屏的设备。你还强迫某些设备(在横屏下工作得最好)进入竖屏,而没有得到正确的方向。

我的建议是用“nosensor”代替。这将使设备使用其默认的首选方向,不会阻止谷歌Play上的任何购买/下载,并将确保传感器不会破坏你的游戏(在我的情况下是NDK)。

在<apphome>/platform/android目录下,创建AndroidManifest.xml(从生成的目录中复制它)。

然后添加android:screenOrientation="portrait"到所有的活动元素。

在你的manifest中添加android:screenOrientation="portrait"。如果您想为特定的活动应用模式,请在所需的活动标记中定义此模式。或者在应用程序标签中定义整个应用程序。比如:

<application
    <!-- ... -->
    android:screenOrientation="portrait"
</application>

你可以通过在manifest.xml文件中这样写来强制你的特定活动始终保持纵向模式:

<activity
    android:name=".MainActivity"
    android:screenOrientation="portrait"></activity>

你也可以通过在你的活动的onCreate()方法中写入下面的行来强制你的活动保持在纵向模式:

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_layout);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

如果你的活动与第一个设备朝向状态相关,在onCreate方法中获取当前设备朝向,然后永远修复它:

int deviceRotation = ((WindowManager) getBaseContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getOrientation();

if(deviceRotation == Surface.ROTATION_0) {
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
else if(deviceRotation == Surface.ROTATION_180)
{
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
}
else if(deviceRotation == Surface.ROTATION_90)
{
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
else if(deviceRotation == Surface.ROTATION_270)
{
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
}