概述
本文翻译自:Prevent screen rotation on Android
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"? 有没有办法告诉此活动“即使用户疯狂地摇晃手机也不要旋转屏幕”?
#1楼
参考:https://stackoom.com/question/BSQ3/防止Android上的屏幕旋转
#2楼
Activity.java Activity.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>
#3楼
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>
#4楼
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
...
...
android:screenOrientation="landscape">
or if you want to lock it in vertical mode: 或者如果您想将其锁定在垂直模式下:
<activity
...
...
android:screenOrientation="portrait">
#5楼
If you are using Android Developer Tools (ADT) and Eclipse you can go to your AndroidManifest.xml --> Application tab --> go down and select your activity. 如果您使用的是Android开发人员工具 (ADT)和Eclipse ,则可以转到AndroidManifest.xml-> 应用程序选项卡->向下并选择您的活动。 Finally, select your preferred orientation. 最后,选择您的首选方向。 You can select one of the many options. 您可以选择许多选项之一。
#6楼
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);
}
最后
以上就是热心帽子为你收集整理的防止Android上的屏幕旋转的全部内容,希望文章能够帮你解决防止Android上的屏幕旋转所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复