orientation is not working in 2.3.3?

微笑、不失礼 提交于 2019-12-29 08:53:05

问题


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 when i change landscape to portrait it is not working.my code is,

  public void onConfigurationChanged(Configuration newConfig) {

        super.onConfigurationChanged(newConfig);
        System.out.println("orientation---"+getResources().getConfiguration().orientation);
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            setContentView(R.layout.main);
            System.out.println("landscape-----");
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            setContentView(R.layout.main);
            System.out.println("portrait-----");
        }

    }

Every time time it taking only landscape mode.I made mistake some where. please help.

System.out.println("orientation---"+getResources().getConfiguration().orientation);

The above SOP is printing 2 means landscape.Please help me.(i am using 2.3.3)


回答1:


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.




回答2:


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



回答3:


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



来源:https://stackoverflow.com/questions/9566305/orientation-is-not-working-in-2-3-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!