orientation is not working in 2.3.3?

前端 未结 3 420
别那么骄傲
别那么骄傲 2020-12-21 09:32

I have prepared one application.My application will be supports both landscape and portrait. When i change the orientation from portrait to landscape it is working fine,But

相关标签:
3条回答
  • 2020-12-21 09:54

    There's multiple reasons why this could happen, hard to tell without seing more code.

    Here's some pointers:

    • you haven't forced the layout to some particular orientation via manifest file or programatically (something like <activity android:screenOrientation="portrait"> in your manifest)

    • by default the Activity is restarted when configuration changes happend. If you want to NOT have your activity restarted when the orientation changes, you need to use

      android:configChanges="orientation"

    on your activity's configuration in the manifest.

    • the above point will in fact make Android call your activity's onConfigurationChanged(Configuration) method when the orientation changes

    ==update==

    Also, if you have the same layout (like "main") declared in layout-land and layout-port you only need to specify that you want to use that layout in the beginning:

     public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
        }
    

    and that's it. THERE IS NO NEED to also implement the

    public void onConfigurationChanged(Configuration newConfig) {
      //...
    }
    

    method as well. For the sake of chosing the correct layout based on orientation all you have to do is declare the same layout in those two folders.

    (Also, there is NO NEED to add the android:configChanges="orientation" to your manifest, apparently it's on by default). I've tried all this just now, works ok

    0 讨论(0)
  • 2020-12-21 10:03

    The issue is with the android:configChanges="orientation|keyboardHidden" .

    if you remove |keyboardHidden from the androidmanifest.xml, then the onConfigurationChanged is only fired when you rotate from landscape to portrait, not when you go from portrait to landscape (at least in the default emulator).

    Hope this helps.

    EDIT: DID some sample program to test it and works fine for me.

    CheckOrientationActivity.java

    import android.content.res.Configuration;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.Toast;
    
    public class CheckOrientationActivity extends Activity {
    
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.second_layout);
            Log.i("DayElevenActivity", "onCreate Start");
        }
    
        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            Log.i("DayElevenActivity", "onConfigurationChanged");
            // Checks the orientation of the screen
            if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
                Log.i("ORIENTATION_LANDSCAPE", "ORIENTATION_LANDSCAPE");
                Toast.makeText(getBaseContext(), "ORIENTATION_LANDSCAPE", Toast.LENGTH_SHORT).show();
            } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
                Log.i("ORIENTATION_PORTRAIT", "ORIENTATION_PORTRAIT");
                Toast.makeText(getBaseContext(), "ORIENTATION_PORTRAIT", Toast.LENGTH_SHORT).show();
            }
        }
    
    }
    

    In AndroidManifest.xml

    <activity
                android:label="@string/app_name"   
                android:configChanges="**orientation|keyboardHidden**"    
                android:name=".CheckOrientationActivity" >
                <intent-filter >
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    

    Corresponding Logs when i run and change the orientation

    03-05 18:54:31.644: I/CheckOrientationActivity (20314): onCreate Start
    03-05 18:54:35.014: I/CheckOrientationActivity (20314): onConfigurationChanged
    03-05 18:54:35.014: I/ORIENTATION_LANDSCAPE(20314): ORIENTATION_LANDSCAPE
    03-05 18:54:41.984: I/CheckOrientationActivity (20314): onConfigurationChanged
    03-05 18:54:41.984: I/ORIENTATION_PORTRAIT(20314): ORIENTATION_PORTRAIT
    
    0 讨论(0)
  • 2020-12-21 10:13

    You might be hitting this bug "Orientation does not change from landscape to portrait on Emulator on 2.3"

    More than 45 people are facing the same issue with the androidd 2.3 emulator

    http://code.google.com/p/android/issues/detail?id=13189

    Host OS: Windows 7 SDK tools version : revision 8 Eclipse version: 3.5 ADT plug-in version: 8.0.1 Platform targeted by your project: 2.3 Version of the platform running in the emulator:2.3

    STEPS TO REPRODUCE: 1. Activity 1 (no screen orientation) - Activity 2 (screen orientation = landscape)

    1. Going from Activity 1 to 2 works fine. Pressing back changes the orientation of Activity 1 to landscape

    EXPECTED RESULTS: Activity 1 should be in portrait mode

    OBSERVED RESULTS: Behavior is observed only in gingerbread, works as expected on froyo.

    0 讨论(0)
提交回复
热议问题