android 屏幕方向检测错误?

发布于 2024-11-26 13:43:17 字数 2887 浏览 4 评论 0原文

我是android新手,在学习基础知识时遇到了这个问题。

我在谷歌和这里搜索了我的问题,但找不到类似的问题。

我的问题是我无法检测屏幕方向何时更改为横向。

这是我从这里获得的代码

public void onConfigurationChanged(Configuration newConfig)
{
    super.onConfigurationChanged(newConfig);

    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
        System.out.println("orientation landscape");
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
        System.out.println("orientation potrait");
    } else{
        Toast.makeText(this, "undefined", Toast.LENGTH_SHORT).show();
        System.out.println("orientation undefined");
    }
}

    <activity android:name=".Hello2011Activity"
              android:label="@string/app_name"
              android:theme="@style/testem"
              android:configChanges="orientation"
              >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

这是日志

07-27 16:19:11.935: INFO/ActivityManager(52): Config changed: { scale=1.0 imsi=310/260 loc=en_US touch=3 keys=2/1/2 nav=3/1 orien=2 layout=18}
07-27 16:19:12.065: INFO/ActivityManager(52): Config changed: { scale=1.0 imsi=310/260 loc=en_US touch=3 keys=2/1/2 nav=3/1 orien=1 layout=18}
07-27 16:19:12.305: INFO/System.out(1241): orientation potrait
07-27 16:19:15.485: INFO/ActivityManager(52): Config changed: { scale=1.0 imsi=310/260 loc=en_US touch=3 keys=2/1/1 nav=3/1 orien=2 layout=18}
07-27 16:27:11.465: INFO/ActivityManager(52): Config changed: { scale=1.0 imsi=310/260 loc=en_US touch=3 keys=2/1/2 nav=3/1 orien=2 layout=18}
07-27 16:27:11.665: INFO/ActivityManager(52): Config changed: { scale=1.0 imsi=310/260 loc=en_US touch=3 keys=2/1/2 nav=3/1 orien=1 layout=18}
07-27 16:27:11.885: INFO/System.out(1241): orientation potrait
07-27 16:27:14.035: INFO/ActivityManager(52): Config changed: { scale=1.0 imsi=310/260 loc=en_US touch=3 keys=2/1/1 nav=3/1 orien=2 layout=18}
07-27 16:27:20.401: INFO/ActivityManager(52): Config changed: { scale=1.0 imsi=310/260 loc=en_US touch=3 keys=2/1/2 nav=3/1 orien=2 layout=18}
07-27 16:27:20.675: INFO/ActivityManager(52): Config changed: { scale=1.0 imsi=310/260 loc=en_US touch=3 keys=2/1/2 nav=3/1 orien=1 layout=18}
07-27 16:27:20.865: INFO/System.out(1241): orientation potrait

正如您所看到的,只有当我将方向从横向更改为纵向时才会收到通知,反之亦然。

我尝试过 api 级别 7 的 avd 2.1update1 和 api 级别 8 的 avd 2.2 。 操作系统是 windows xp sp 3 和 eclipse 3.6.2。

这是一个错误还是代码有问题?

I'm new to android, I encountered this problem while learning the basics.

I've searched for my problem in google and here but can't find similiar one.

My problem is I can't detect when the screen orientation changed to landscape.

Here's the code I got from here

public void onConfigurationChanged(Configuration newConfig)
{
    super.onConfigurationChanged(newConfig);

    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
        System.out.println("orientation landscape");
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
        System.out.println("orientation potrait");
    } else{
        Toast.makeText(this, "undefined", Toast.LENGTH_SHORT).show();
        System.out.println("orientation undefined");
    }
}

    <activity android:name=".Hello2011Activity"
              android:label="@string/app_name"
              android:theme="@style/testem"
              android:configChanges="orientation"
              >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

Here's the log

07-27 16:19:11.935: INFO/ActivityManager(52): Config changed: { scale=1.0 imsi=310/260 loc=en_US touch=3 keys=2/1/2 nav=3/1 orien=2 layout=18}
07-27 16:19:12.065: INFO/ActivityManager(52): Config changed: { scale=1.0 imsi=310/260 loc=en_US touch=3 keys=2/1/2 nav=3/1 orien=1 layout=18}
07-27 16:19:12.305: INFO/System.out(1241): orientation potrait
07-27 16:19:15.485: INFO/ActivityManager(52): Config changed: { scale=1.0 imsi=310/260 loc=en_US touch=3 keys=2/1/1 nav=3/1 orien=2 layout=18}
07-27 16:27:11.465: INFO/ActivityManager(52): Config changed: { scale=1.0 imsi=310/260 loc=en_US touch=3 keys=2/1/2 nav=3/1 orien=2 layout=18}
07-27 16:27:11.665: INFO/ActivityManager(52): Config changed: { scale=1.0 imsi=310/260 loc=en_US touch=3 keys=2/1/2 nav=3/1 orien=1 layout=18}
07-27 16:27:11.885: INFO/System.out(1241): orientation potrait
07-27 16:27:14.035: INFO/ActivityManager(52): Config changed: { scale=1.0 imsi=310/260 loc=en_US touch=3 keys=2/1/1 nav=3/1 orien=2 layout=18}
07-27 16:27:20.401: INFO/ActivityManager(52): Config changed: { scale=1.0 imsi=310/260 loc=en_US touch=3 keys=2/1/2 nav=3/1 orien=2 layout=18}
07-27 16:27:20.675: INFO/ActivityManager(52): Config changed: { scale=1.0 imsi=310/260 loc=en_US touch=3 keys=2/1/2 nav=3/1 orien=1 layout=18}
07-27 16:27:20.865: INFO/System.out(1241): orientation potrait

As you can see there's only notification for when I change the orientation changed from landscape to potrait and not vice versa.

I've tried avd 2.1update1 with api level 7 and avd 2.2 with api level 8.
OS is windows xp sp 3 with eclipse 3.6.2.

Is this a bug or there's something wrong with the code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

十秒萌定你 2024-12-03 13:43:17

我遇到了同样的问题,但使用orientation|keyboardHidden 没有解决它。有人有主意吗?

编辑:
经过一番尝试和错误后,我发现这对我有用:

android:configChanges="keyboardHidden|orientation|screenSize"

I'm having the same problem but using orientation|keyboardHidden didn't solve it. anyone has an idea?

EDIT:
After some trial and error, I found that this works for me:

android:configChanges="keyboardHidden|orientation|screenSize"
鱼忆七猫命九 2024-12-03 13:43:17

activity-restart-on-rotation-android

how-do-i-detect-screen-rotation 并使用Activity 的 android:screenOrientation="portrait" 用于不更改方向(这可以与 AndroidManifest.xml 中的 Activity 标记一起使用),如下

<activity android:name="testActivity"
            android:screenOrientation="portrait" android:label="@string/app_name">

(使用此设备后改变方向没有意义)

activity-restart-on-rotation-android

how-do-i-detect-screen-rotation and use android:screenOrientation="portrait" to activity is use for not make change of orientation (this can use with the activity tag into the AndroidManifest.xml) like this

<activity android:name="testActivity"
            android:screenOrientation="portrait" android:label="@string/app_name">

(after using this the device will not make sense on change of the orientation)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文