【问题标题】:android screen orientation detect bug? [duplicate]android屏幕方向检测错误? [复制]
【发布时间】:2011-07-27 09:22:00
【问题描述】:

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

我在 google 和 here 中搜索过我的问题,但找不到类似的问题。

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

这是我从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>

这是日志

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。 操作系统是带有 eclipse 3.6.2 的 windows xp sp 3。

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

【问题讨论】:

  • 我只是看看,你的源代码看起来不错。这看起来很奇怪,你能否将完整的源代码发送到我的电子邮件,非常有趣的问题:)
  • 我也试过代码。方法onConfigurationChanged 未在横向模式下执行。
  • @nguyendat 我创建了一个新项目来测试它,但它仍然无法正常工作。这是项目代码s000.tinyupload.com/index.php?file_id=64677725252486745434
  • @Mudassir 我以为只有我一个人有这个问题 :)
  • android:screenOrientation="portrait" 到活动用于不改变方向

标签: android android-emulator android-orientation


【解决方案1】:

我遇到了同样的问题,但使用方向|键盘隐藏并没有解决它。有人有想法吗?

编辑: 经过反复试验,我发现这对我有用:

android:configChanges="keyboardHidden|orientation|screenSize"

【讨论】:

【解决方案2】:

activity-restart-on-rotation-android
how-do-i-detect-screen-rotation 和使用android:screenOrientation="portrait" 来活动是用于不改变方向(这可以与活动标签一起使用到AndroidManifest.xml)像这样

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

(使用此设备后,设备将无法更改方向)

【讨论】:

  • 我不想强制屏幕方向,我只想检测方向是否改变。但感谢您的信息。无论如何,我已经得到了答案(请参阅下面的帖子),但我不知道如何关闭此线程。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-16
  • 1970-01-01
  • 1970-01-01
  • 2014-05-18
  • 1970-01-01
  • 2011-06-08
  • 1970-01-01
相关资源
最近更新 更多