android screen orientation

2019-01-09 09:25发布

I tried getOrientation() to get the orientation value but it always returns 0!

8条回答
太酷不给撩
2楼-- · 2019-01-09 09:25

getOrientation() is deprecated. Instead, try getRotation().

查看更多
ら.Afraid
3楼-- · 2019-01-09 09:28
/* First, get the Display from the WindowManager */
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();

/* Now we can retrieve all display-related infos */
int width = display.getWidth();
int height = display.getHeight();
int orientation = display.getOrientation();
查看更多
SAY GOODBYE
4楼-- · 2019-01-09 09:37

I had the same problem on NexusOne SDK 2.3.

This solved it: Check orientation on Android phone.

查看更多
男人必须洒脱
5楼-- · 2019-01-09 09:40

If you want to know if the content currently displayed is in landscape mode or portrait (possibly completely independent of the phone's physical rotation) you can use:

getResources().getConfiguration().orientation

Developer Documentation

public int orientation
Since: API Level 1

Overall orientation of the screen. May be one of ORIENTATION_LANDSCAPE, ORIENTATION_PORTRAIT, or ORIENTATION_SQUARE.

查看更多
淡お忘
6楼-- · 2019-01-09 09:40

To avoid use of Deprecated methods use the Android Configuration class found here. It works since API lvl 1 and still works on the latest android devices. (Not deprecated).

As and example consider the following code snippet:

Configuration config = getResources().getConfiguration();
if (config.orientation == Configuration.ORIENTATION_PORTRAIT)
{
    setContentView(R.layout.portraitStart);
}
else
{
    setContentView(R.layout.landscapeStart);
}

Best of luck- hope this answer helps whoever runs into it.

查看更多
家丑人穷心不美
7楼-- · 2019-01-09 09:45

getOrientation() is deprecated but this is not neccesarily the root of your problem. It is true that you should use getRotation() instead of getOrientation() but you can only use it if you are targeting Android 2.2 (API Level 8) or higher. Some people and even Googlers sometimes seem to forget that.

As an example, on my HTC desire running Android 2.2. getOrientation() and getRotation() both report the same values:

  • 0 (default, portrait),
  • 1 (device 90 degree counterclockwise),
  • 3 (device 90 degree clockwise)

It does not report when you put it "on its head" (rotate 180, that would be the value 2). This result is possibly device-specific.

First of all you should make clear if you use an emulator or a device. Do you know how to rotate the emulator? Then I would recommend to create a small test program with a onCreate() Method like this:

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

    WindowManager mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    Display mDisplay = mWindowManager.getDefaultDisplay();

    Log.d("ORIENTATION_TEST", "getOrientation(): " + mDisplay.getOrientation());

}

Check if the screen of your your device has been locked in the device settings Settings > Display > Auto-Rotate Screen. If that checkbox is unchecked, Android will not report orientation changes to your Activity. To be clear: it will not restart the activity. In my case I get only 0, like you described.

You can check this from your program if you add these lines to onCreate()

int sysAutoRotate = 0;
try {
        sysAutoRotate = Settings.System.getInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION);
    } catch (SettingNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
Log.d("ORIENTATION_TEST", "Auto-rotate Screen from Device Settings:" + sysAutoRotate);

It will return 0 if Auto-Rotate is off and 1 if Auto-Rotate is on. Another try. In your original program you might have set in manifest

android:screenOrientation="portrait"

Same effect, but this time for your activity only. If you made the small test program this possibilty would have been eliminated (that's why I recommend it).

Remark: Yes the whole orientation / rotation topic is an "interesting" topic indeed. Try things out, use Log.d(), experiment, learn.

查看更多
登录 后发表回答