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

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


当前回答

Add

android:screenOrientation="portrait" 

or

 android:screenOrientation="landscape" 

到<activity>元素/s中 拿到载货单,你就完了。

其他回答

在你的Manifest文件中,对于每个你想锁定屏幕旋转的Activity添加:如果你想锁定在水平模式:

<activity
        ...
        ...
        android:screenOrientation="landscape">

或者如果你想锁定在垂直模式:

<activity
            ...
            ...
            android:screenOrientation="portrait">

Add

android:screenOrientation="portrait" 

or

 android:screenOrientation="landscape" 

到<activity>元素/s中 拿到载货单,你就完了。

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

and

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

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

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);
}

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

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

这对我很有用。