Android的屏幕方向 [英] android screen orientation

查看:142
本文介绍了Android的屏幕方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过 getOrientation()获得的价值取向,但它总是返回0!

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

推荐答案

getOrientation()是pcated德$ P $但这不是neccesarily你的问题的根源。这是事实,你应该使用 getRotation()而不是 getOrientation(),但你只能用它,如果你的目标的Andr​​oid 2.2(API等级8级)或更高。有些人甚至谷歌有时似乎忘记了这一点。

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.

作为一个例子,在我运行的是Android 2.2 HTC渴望。 getOrientation() getRotation()这两个报告相同的值:

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

  • 0(默认情况下,纵向),
  • 1(设备90度逆时针),
  • 3(设备顺时针旋转90度)

当你把它发挥得淋漓尽致不报告(旋转180,这将是价值2)。这个结果可能是特定于设备的。

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.

首先,如果您使用的仿真器或设备,你应该清楚。你知道如何旋转模拟器?然后,我会建议使用这样的的onCreate()方法创建一个小的测试程序:

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());

}

检查,如果你对你的设备的屏幕一直是锁定在设备设置设置>显示>自动旋转屏幕。如果该复选框被选中,Android将不报告的方向改变你的活动。需要明确的是:它不会重新启动该活动。在我来说,我只得到 0 ,像你这样的描述。

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.

您可以检查该从你的程序,如果你添加这些行的onCreate()

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);

这将返回 0 如果自动旋转是关闭和 1 如果自动旋转开启。 另一种尝试。在原来的计划,您可能会在明显的设置

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"

同样的效果,但这次只有你的活动。如果你做了小测试程序,这个possibilty将被淘汰(这就是为什么我把它推荐)。

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).

注:是的整个方向/旋转的话题是一个有趣的话题确实如此。尝试的东西出来,用Log.d(),实验,学习。

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

这篇关于Android的屏幕方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆