"风景"和"风景反转"方向的不同布局

Ram*_*ami 6 android landscape orientation device-orientation orientation-changes

我的问题:

对于某些要求,我的活动需要两种不同的xml布局:

  • 一个用于风景模式.
  • 另一个用于景观反转模式(颠倒景观).

不幸的是Android不容许景观反向创建单独的布局(如我们可以为纵向和横向与做layout-landlayout-port).

AFAIK,唯一的方法是从java代码更改activity-xml.

我尝试过的:

1)覆盖onConfigurationChanged()方法来检测方向变化,但我无法弄清楚它是横向还是横向反转:

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

     if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
         Log.d("TEST","Landscape");
     }

}
Run Code Online (Sandbox Code Playgroud)

(android:configChanges="keyboardHidden|orientation|screenSize|layoutDirection"在清单中我的活动标签中的whith)

2)使用此答案中建议的OrientationEventListenerwith SENSOR_DELAY_NORMAL,但在进入我的块之前设备方向会发生变化,因此我得到了视图的延迟更新:if

mOrientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL){

            @Override
            public void onOrientationChanged(int orientation) {

                if (orientation==0){
                    Log.e("TEST", "orientation-Portrait  = "+orientation);
                } else if (orientation==90){
                    Log.e("TEST", "orientation-Landscape = "+orientation);
                } else if(orientation==180){
                    Log.e("TEST", "orientation-Portrait-rev = "+orientation);
                }else if (orientation==270){
                    Log.e("TEST", "orientation-Landscape-rev = "+orientation);
                } else if (orientation==360){
                    Log.e("TEST", "orientation-Portrait= "+orientation);
                }

            }};
Run Code Online (Sandbox Code Playgroud)

我的问题:

是否有更好的解决方案来改变活动布局"Landscape""Landscape-reverse"方向?

任何建议都非常感谢.

小智 5

在这里按照建议尝试吗?您可以使用activity属性处理具有不同类型的配置reverse和standart的事件sensorLandscape

编辑:尝试使用此处所述的Display.getOrientation http://android-developers.blogspot.in/2010/09/one-screen-turn-deserves-another.html

并且不要忘记configChanges在清单中的活动上设置标志以手动处理更改onConfigurationChanges().

因此,似乎只有这样才能尽可能频繁地监听SensorManager.

SensorManager sensorMan = (SensorManager)getSystemService(SENSOR_SERVICE);
Sensor sensor = sensorMan.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorMan.registerListener(...)
Run Code Online (Sandbox Code Playgroud)