倒挂旋转不会触发配置更改 [英] Upside down rotation doesn't trigger configuration change

查看:146
本文介绍了倒挂旋转不会触发配置更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我把我的Andr​​oid手机上下颠倒,我的活动不旋转,显示布局倒挂,但在横向模式,而不是停留。我想这有一个很简单的HelloWorld应用程序。我加了的android:configChanges =方向来的清单和压倒 onConfigurationChange()在活动中设置断点有。旋转设备倒挂产生一个配置变化从纵向走(上行上)景观但是从横向到纵向不第二个变化(倒置)。这是一个Android的问题,或者是有什么我需要做什么?

清单:

 <?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=hello.world
    安卓版code =1
    机器人:=的versionName1.0>
   <采用-SDK安卓的minSdkVersion =10/>
    <应用
        机器人:图标=@绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME>
        <活动
            机器人:configChanges =方向
            机器人:名字=。HelloWorldActivity
            机器人:标签=@字符串/ APP_NAME>
            &所述;意图滤光器>
                <作用机器人:名字=android.intent.action.MAIN/>
                <类机器人:名字=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
    < /用途>
< /清单>

活动:

 公共类HelloWorldActivity
  扩展活动
{
  / **当第一次创建活动调用。 * /
  @覆盖
  公共无效的onCreate(捆绑savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
  }  公共无效onConfigurationChanged(配置NEWCONFIG)
  {
    super.onConfigurationChanged(NEWCONFIG);
    如果(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
    {
      Log.e(MDO,方向转变:山水);
    }
    否则,如果(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
    {
      Log.e(MDO,方向改变:画像);
    }
  }
}


解决方案

这不是一个问题,它是如何的Andr​​oid工程。它认为颠倒只在横向模式而不是纵向(在2.2版本的景观,如果我没记错的话)。当它从纵向变为横向到,反之亦然发生配置更改。如果要检查电话被翻转倒置或任何方向,你必须访问加速计传感器。下面是一个<一个href=\"http://www.techrepublic.com/blog/app-builder/a-quick-tutorial-on-coding-androids-accelerometer/472\"相对=nofollow>教程如何使用它,在这里你拥有的http://developer.android.com/reference/android/hardware/SensorManager.html相对=nofollow > 的SensorManager 文档

编辑:这个问题笔者发现了自己,加入的android:screenOrientation =fullSensor你的清单是不够的,只要你不不想支持任何年龄超过了Android 2.3(API等级9)

When I turn my Android phone upside down, my Activity does not rotate to show the layout upside down but stays in landscape mode instead. I tried this with a very simple HelloWorld app. I added android:configChanges="orientation" to the manifest and overrode onConfigurationChange() in the Activity to set breakpoints there. Rotating the device upside down produces one config change to go from portrait (upside up) to landscape but no second change from landscape to portrait (upside down). Is this an Android issue or is there something I need to do?

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="hello.world"
    android:versionCode="1"
    android:versionName="1.0" >
   <uses-sdk android:minSdkVersion="10" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:configChanges="orientation"
            android:name=".HelloWorldActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Activity:

public class HelloWorldActivity
  extends Activity
{
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
  }

  public void onConfigurationChanged(Configuration newConfig)
  {
    super.onConfigurationChanged(newConfig);
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
    {
      Log.e("MDO", "orientation change: landscape");
    }
    else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
    {
      Log.e("MDO", "orientation change: portrait");
    }
  }
}

解决方案

It's not an issue, it's how Android works. It views upside-down only in landscape mode and not in portrait (and in landscape from version 2.2 if I remember correctly). Configuration change occurs when it goes from portrait to landscape and vice versa. If you want to check if the phone was flipped upside down or in whatever direction you'll have to access accelerometer sensor. Here's a tutorial on how to use it and here you have SensorManager docs.

EDIT: as the author of the question found out himself, adding android:screenOrientation="fullSensor" to your manifest is enough, provided that you don't want to support anything older than Android 2.3 (API level 9).

这篇关于倒挂旋转不会触发配置更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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