首页 文章

如何在Android上禁用方向更改?

提问于
浏览
270

我有一个我想在纵向模式下使用的应用程序,所以我在清单XML中定义了android:screenOrientation = "portrait" . 这适用于HTC Magic手机(并防止其他手机上的方向更改) .

但是当我打开硬件QWERTY keyboard(而不是虚拟键盘)时,HTC G1手机出现问题 . 我的活动保持纵向模式,但它似乎重新启动并失去所有状态 . HTC Hero版本不会发生这种情况 .

我的应用程序非常大,所以我不希望它在键盘打开时重新启动并丢失所有状态 . 我怎么能防止这种情况?

12 回答

  • 94

    Update April 2013: Don't do this. It wasn't a good idea in 2009 when I first answered the question and it really isn't a good idea now. See this answer by hackbod for reasons:

    Avoid reloading activity with asynctask on orientation change in android

    android:configChanges="keyboardHidden|orientation" 添加到AndroidManifest.xml . 这告诉系统您将自己处理哪些配置更改 - 在这种情况下无需执行任何操作 .

    <activity android:name="MainActivity"
         android:screenOrientation="portrait"
         android:configChanges="keyboardHidden|orientation">
    

    有关更多详细信息,请参阅开发人员参考configChanges .

    但是,您的申请可以随时中断,例如通过电话,所以你真的应该添加代码来保存应用程序暂停时的状态 .

    Update: 从Android 3.2开始,您还需要添加"screenSize":

    <activity
        android:name="MainActivity"
        android:screenOrientation="portrait"
        android:configChanges="keyboardHidden|orientation|screenSize">
    

    来自开发者指南Handling the Configuration Change Yourself

    注意:从Android 3.2(API级别13)开始,当设备在纵向和横向之间切换时,“屏幕尺寸”也会发生变化 . 因此,如果要在开发API级别13或更高级别(由minSdkVersion和targetSdkVersion属性声明)时由于方向更改而阻止运行时重新启动,则除了“orientation”值之外,还必须包含“screenSize”值 . 也就是说,你必须声明android:configChanges =“orientation | screenSize” . 但是,如果您的应用程序的目标是API级别12或更低,那么您的活动始终会自行处理此配置更改(即使在Android 3.2或更高版本的设备上运行,此配置更改也不会重新启动您的活动) .

  • 20

    你需要修改AndroidManifest.xml为Intrications(之前的Ashton),并确保活动处理你想要它处理的onConfigurationChanged事件 . 它应该是这样的:

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
    
  • 2

    我总是发现你需要两者

    android:screenOrientation="nosensor" android:configChanges="keyboardHidden|orientation"
    
  • 308

    如上所述,将您的活动的 android:configChanges (在清单文件中)设置为 keyboardHidden|orientation ,然后:

    1)覆盖 onConfigurationChanged()

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        //here you can handle orientation change
    }
    

    2)将此行添加到您的活动的 onCreate()

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    

    这比将相同的行添加到 onConfigurationChanged 更好,因为你的应用程序将转为纵向模式,然后回到横向(它只会发生一次,但它很烦人) .

    您也可以为活动设置 android:screenOrientation="nosensor" (在清单中) . But 使用这种方式你根本无法处理方向变化 .

  • 39

    用这个..

    android:screenOrientation="portrait"
    
  • 10

    在您的活动的OnCreate方法中使用以下代码:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    

    现在,您的方向将设置为纵向,永远不会改变 .

  • 8

    在AndroidManifest.xml文件中,对于要锁定的每个活动,添加最后一个 screenOrientation 行:

    android:label="@string/app_name"
    android:name=".Login"
    android:screenOrientation="portrait" >
    

    android:screenOrientation="landscape" .

  • 0

    androidmanifest.xml 文件中:

    <activity android:name="MainActivity" android:configChanges="keyboardHidden|orientation">
    

    要么

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
    
  • 0

    要通过代码锁定屏幕,您必须使用屏幕的实际旋转(0,90,180,270),您必须知道它的自然位置,在智能手机中,自然位置将是纵向和平板电脑,这将是风景 .

    这是代码(锁定和解锁方法),它已经在一些设备(智能手机和平板电脑)中进行了测试,效果很好 .

    public static void lockScreenOrientation(Activity activity)
    {   
        WindowManager windowManager =  (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);   
        Configuration configuration = activity.getResources().getConfiguration();   
        int rotation = windowManager.getDefaultDisplay().getRotation(); 
    
        // Search for the natural position of the device    
        if(configuration.orientation == Configuration.ORIENTATION_LANDSCAPE &&  
           (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) ||  
           configuration.orientation == Configuration.ORIENTATION_PORTRAIT &&   
           (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270))   
        {   
            // Natural position is Landscape    
            switch (rotation)   
            {   
                case Surface.ROTATION_0:    
                    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);    
                    break;      
                case Surface.ROTATION_90:   
                    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); 
                break;      
                case Surface.ROTATION_180: 
                    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); 
                    break;          
                case Surface.ROTATION_270: 
                    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
                    break;
            }
        }
        else
        {
            // Natural position is Portrait
            switch (rotation) 
            {
                case Surface.ROTATION_0: 
                    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
                break;   
                case Surface.ROTATION_90: 
                    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
                break;   
                case Surface.ROTATION_180: 
                    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); 
                    break;          
                case Surface.ROTATION_270: 
                    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); 
                    break;
            }
        }
    }
    
    public static void unlockScreenOrientation(Activity activity)
    {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
    }
    
  • 5

    在Visual Studio Xamarin中:

    • 添加:

    using Android.Content.PM; 到您的活动命名空间列表 .

    • 添加:

    [Activity(ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait)]

    作为你的类的属性,像这样:

    [Activity(ScreenOrientation = ScreenOrientation.Portrait)]
    public class MainActivity : Activity
    {...}
    
  • 22

    android:configChanges="keyboardHidden|orientation|screenSize"
    

    到你的清单 .

  • 13

    请注意,现在没有一种方法可行!

    Android Studio 1中,一种简单的方法是添加 android:screenOrientation="nosensor" .

    这有效地锁定了屏幕方向 .

相关问题