android 屏幕方向检测错误?
我是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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了同样的问题,但使用orientation|keyboardHidden 没有解决它。有人有主意吗?
编辑:
经过一番尝试和错误后,我发现这对我有用:
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:
activity-restart-on-rotation-android
how-do-i-detect-screen-rotation 并使用Activity 的
android:screenOrientation="portrait"
用于不更改方向(这可以与AndroidManifest.xml
中的 Activity 标记一起使用),如下(使用此设备后改变方向没有意义)
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 theAndroidManifest.xml
) like this(after using this the device will not make sense on change of the orientation)