我有一个应用程序,我想在纵向模式下使用,所以我已经定义了 android:screenOrientation=“portrait”在manifest XML中。这适用于HTC Magic手机(并防止方向变化的其他手机以及)。

但当我打开硬件QWERTY键盘(不是虚拟键盘)时,HTC G1手机遇到了一个问题。我的活动保持在肖像模式,但它似乎重新启动,失去了所有的状态。HTC Hero版本不会出现这种情况。

我的应用程序相当大,所以我不希望它在打开键盘时重新启动并失去所有状态。我该如何预防呢?


2013年4月更新:不要这样做。在2009年我第一次回答这个问题时,这并不是一个好主意,现在也确实不是一个好主意。请看hackbod的回答,原因如下:

避免在android中方向改变时用asynctask重新加载活动

添加android:configChanges="keyboardHidden|orientation"到你的AndroidManifest.xml。这将告诉系统您自己将要处理哪些配置更改—在本例中什么也不做。

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

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

然而,你的应用程序可能在任何时候被中断,例如被一个电话打断,所以你真的应该添加代码来保存应用程序暂停时的状态。

更新:从Android 3.2开始,你还需要添加“screenSize”:

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

来自开发人员指南自己处理配置更改

Caution: Beginning with Android 3.2 (API level 13), the "screen size" also changes when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), you must include the "screenSize" value in addition to the "orientation" value. That is, you must declare android:configChanges="orientation|screenSize". However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).

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

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

我一直觉得你两者都需要

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

在你的androidmanifest.xml文件中:

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

or

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

如上所述,设置android:configChanges你的活动(在manifest文件)到keyboardHidden|方向,然后:

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"(在manifest中)。但是用这种方法你根本不能处理方向的变化。

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

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

现在你的方向将被设置为竖屏,永远不会改变。

使用这个. .

    android:screenOrientation="portrait"

要通过代码锁定屏幕,你必须使用屏幕的实际旋转(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);
}

Add

android:configChanges="keyboardHidden|orientation|screenSize" 

到您的舱单上。

在AndroidManifest.xml文件中,为你想锁定的每个活动添加最后一行screenOrientation:

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

或android: screenOrientation =“景观”。

请注意,现在这些方法似乎都不起作用!

在Android Studio 1中,一个简单的方法就是添加 android: screenOrientation =“nosensor”。

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

在Visual Studio Xamarin中:

添加:

使用Android.Content.PM;到您的活动名称空间列表。

添加:

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

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

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

如果你想在你的应用程序中的所有活动都只使用纵向模式,你可以简单地在应用程序类中使用如下所示。

class YourApplicationName : Application() {

override fun onCreate() {
    super.onCreate()

    registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {

        override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
            activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        }

        override fun onActivityStarted(activity: Activity) {

        }

        override fun onActivityResumed(activity: Activity) {

        }

        override fun onActivityPaused(activity: Activity) {

        }

        override fun onActivityStopped(activity: Activity) {

        }

        override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {

        }

        override fun onActivityDestroyed(activity: Activity) {

        }

    })

}

}