防止Android上的屏幕旋转

本文翻译自: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);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值