我有一个我想阻止旋转的活动,因为我正在启动一个AsyncTask,屏幕旋转使它重新启动。

有没有办法告诉这个活动“即使用户像疯了一样摇晃他的手机也不要旋转屏幕”?


当前回答

防止屏幕旋转只需在清单中添加以下一行。

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

这对我很有用。

其他回答

防止屏幕旋转只需在清单中添加以下一行。

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

这对我很有用。

android: screenOrientation =“肖像” 在应用程序标签<application

and

<活动 ... ... android: screenOrientation = "锁定" >

在AndroidManifest.xml中,ACTIVITY的以下属性就是你所需要的:

android:configChanges="orientation"

因此,完整的活动节点将是:

<activity android:name="Activity1"
          android:icon="@drawable/icon"
          android:label="App Name"
          android:excludeFromRecents="true"
          android:configChanges="orientation">
    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
    </intent-filter>
</activity>

如果您正在使用Android开发工具(ADT)和Eclipse 你可以到androidmanifest。xml ->应用选项卡->,然后选择你的活动。最后,选择你喜欢的方向。 您可以从众多选项中选择一个。

我发现最简单的方法就是把

this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

在onCreate中,就在后面

setContentView(R.layout.activity_main);

所以…

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}