在 Android 上防止屏幕旋转
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2730855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Prevent screen rotation on Android
提问by Sephy
I have one of my activities which I would like to prevent from rotating because I'm starting an AsyncTask, and screen rotation makes it restart.
我有一项我想防止旋转的活动,因为我正在启动 AsyncTask,而屏幕旋转使其重新启动。
Is there a way to tell this activity "DO NOT ROTATE the screen even if the user is shaking his phone like mad"?
有没有办法告诉这个活动“即使用户疯狂地摇晃手机,也不要旋转屏幕”?
回答by lbedogni
Add
添加
android:screenOrientation="portrait"
or
或者
android:screenOrientation="landscape"
to the <activity>
element/s in
the manifest and you're done.
到<activity>
清单中的元素,你就完成了。
回答by Emre Yazici
You can follow the logic below to prevent auto rotate screen whileyour AsyncTask
is running:
你可以按照下面的逻辑,以防止自动旋转屏幕,而你AsyncTask
正在运行:
- Store your current screen orientation inside your activity using
getRequestedOrientation()
. - Disable auto screen orientation using
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR)
. - Run/execute your
AsyncTask
. - At the end of your
AsyncTask
restore your previous orientation status usingsetRequestedOrientation(oldOrientation)
.
- 使用 将您当前的屏幕方向存储在您的活动中
getRequestedOrientation()
。 - 使用 禁用自动屏幕方向
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR)
。 - 运行/执行您的
AsyncTask
. - 在您结束时
AsyncTask
使用 恢复您以前的方向状态setRequestedOrientation(oldOrientation)
。
Please note that there are several ways to access Activity
(which runs on UI thread) properties inside an AsyncTask
. You can implement your AsyncTask
as an inner class or you can use message Handler
that poke your Activiy
class.
请注意,有多种方法可以访问Activity
(在 UI 线程上运行)一个AsyncTask
. 你可以将你的实现AsyncTask
为一个内部类,或者你可以使用Handler
戳你的Activiy
类的消息。
回答by Sara
In your Manifest file, for each Activity that you want to lock the screen rotation add: if you want to lock it in horizontal mode:
在您的清单文件中,对于每个要锁定屏幕旋转的 Activity 添加:如果要在水平模式下锁定它:
<activity
...
...
android:screenOrientation="landscape">
or if you want to lock it in vertical mode:
或者如果你想在垂直模式下锁定它:
<activity
...
...
android:screenOrientation="portrait">
回答by Paul Alexander
The easiest way I found to do this was to put
我发现这样做的最简单方法是把
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
within onCreate, just after
在 onCreate 内,就在之后
setContentView(R.layout.activity_main);
so...
所以...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
回答by Antuan Develle Claude
Rather than going into the AndroidManifest, you could just do this:
而不是进入 AndroidManifest,你可以这样做:
screenOrientation = getResources().getConfiguration().orientation;
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
... AsyncTask
screenOrientation = getResources().getConfiguration().orientation;
@Override
protected void onPostExecute(String things) {
context.setRequestedOrientation(PlayListFragment.screenOrientation);
or
context.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
}
The only drawback here is that it requires API level 18 or higher. So basically this is the tip of the spear.
这里唯一的缺点是它需要 API 级别 18 或更高。所以基本上这是矛的尖端。
回答by Li Che
Activity.java
活动.java
@Override
public void onConfigurationChanged(Configuration newConfig) {
try {
super.onConfigurationChanged(newConfig);
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
// land
} else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
// port
}
} catch (Exception ex) {
}
AndroidManifest.xml
AndroidManifest.xml
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="QRCodeActivity" android:label="@string/app_name"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
回答by Ben
The following attribute on the ACTIVITY in AndroidManifest.xml is all you need:
您只需要 AndroidManifest.xml 中 ACTIVITY 的以下属性:
android:configChanges="orientation"
So, the full activity node would be:
因此,完整的活动节点将是:
<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>
回答by Anas
Add:
添加:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
...
...
...
}
回答by andrewoodleyjr
Add the following to your AndroidManifest.xml
将以下内容添加到您的 AndroidManifest.xml
[ app > src > main > AndroidManifest.xml ]
[应用程序> src> 主> AndroidManifest.xml ]
<activity android:name=".MainActivity"
android:configChanges="orientation"
android:screenOrientation="portrait"/>
Example:
例子:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="xxx.zzzzzz.yyyyy">
<uses-permission android:name="A-PERMISSION" />
<application>
<activity android:name=".MainActivity"
android:screenOrientation="portrait"
android:configChanges="orientation">
</activity>
</application>
</manifest>
回答by Bbake Waikhom
Use AsyncTaskLoaderto keep your data safe even if the activity changes, instead of using AsyncTaskthat is a better way to build apps than preventing screen rotation.
即使 Activity 发生变化,使用AsyncTaskLoader 也能确保数据安全,而不是使用AsyncTask,后者是构建应用程序的更好方法,而不是阻止屏幕旋转。