Diseño diferente para la orientación "Paisaje" y "Orientación horizontal"

Mi problema:

Para algunos requisitos necesito dos diseños xml diferentes para mi actividad:

  • Uno para el modo de paisaje .
  • Y otra para el modo Landscape-reverse (al revés de Landscape).

Desafortunadamente Android no permite crear un diseño separado para el paisaje-reverso (como podemos hacer para el retrato y el paisaje con el layout-land y el layout-port ).

AFAIK, la única manera es cambiar la actividad-xml del código java.

Lo que he intentado:

1) onConfigurationChanged() método onConfigurationChanged() para detectar cambios de orientación, pero no puedo averiguar si es Paisaje o Paisaje inverso:

 @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { Log.d("TEST","Landscape"); } } 

(Con android:configChanges="keyboardHidden|orientation|screenSize|layoutDirection" en mi etiqueta de actividad en manifiesto)

2) Utilice un OrientationEventListener con SENSOR_DELAY_NORMAL como se sugiere en esta respuesta, pero la orientación del dispositivo cambia antes de entrar en los bloques if , así que obtengo una actualización retardada de la vista:

 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); } }}; 

Mi pregunta:

¿Existe una mejor solución para cambiar la distribución de las actividades entre la orientación "Landscape" y "Landscape-reverse" Orientación "Landscape-reverse" ?

Cualquier sugerencia es muy apreciada.

¿Estás tratando como se sugiere aquí ? Puede manejar un evento con diferentes tipos de configuración inversa y standart mediante el atributo de actividad sensorLandscape

EDITADO: Intente usar Display.getOrientation como se describe aquí http://android-developers.blogspot.in/2010/09/one-screen-turn-deserves-another.html

Y no se olvide de establecer el indicador configChanges en la actividad en el manifiesto para manejar los cambios manualmente en onConfigurationChanges() .

Así que parece que la única manera de hacerlo es escuchar SensorManager tan frecuentemente como sea posible.

 SensorManager sensorMan = (SensorManager)getSystemService(SENSOR_SERVICE); Sensor sensor = sensorMan.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); sensorMan.registerListener(...) 

Con el fin de lograr este objetivo, es necesario implementar un oyente de rotación, también necesita saber que Android destruir objetos y volver a crearlos para cargar sus diseños, los valores … basado en la configuración de calificadores

PASO 01: crear una interfaz Java [rotationCallbackFn]

  public interface rotationCallbackFn { void onRotationChanged(int lastRotation, int newRotation); } 

PASO 02: crear una clase Java [rotationListenerHelper]

 import android.content.Context; import android.hardware.SensorManager; import android.view.OrientationEventListener; import android.view.WindowManager; public class rotationListenerHelper { private int lastRotation; private WindowManager windowManager; private OrientationEventListener orientationEventListener; private rotationCallbackFn callback; public rotationListenerHelper() { } public void listen(Context context, rotationCallbackFn callback) { // registering the listening only once. stop(); context = context.getApplicationContext(); this.callback = callback; this.windowManager = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); this.orientationEventListener = new OrientationEventListener(context, SensorManager.SENSOR_DELAY_NORMAL) { @Override public void onOrientationChanged(int orientation) { WindowManager localWindowManager = windowManager; rotationCallbackFn localCallback = rotationListenerHelper.this.callback; if(windowManager != null && localCallback != null) { int newRotation = localWindowManager.getDefaultDisplay().getRotation(); if (newRotation != lastRotation) { localCallback.onRotationChanged(lastRotation, newRotation); lastRotation = newRotation; } } } }; this.orientationEventListener.enable(); lastRotation = windowManager.getDefaultDisplay().getRotation(); } public void stop() { if(this.orientationEventListener != null) { this.orientationEventListener.disable(); } this.orientationEventListener = null; this.windowManager = null; this.callback = null; } 

}

PASO 03: agregue estas declaraciones a su mainActivity

 // declaration private rotationListenerHelper rotationListener = null; private Context mContext; //... /* constructor ----------------------------------------------------------------*/ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mContext = this; final int curOrientation = getWindowManager().getDefaultDisplay().getRotation(); switch (curOrientation) { case 0: //. SCREEN_ORIENTATION_PORTRAIT setContentView(R.layout.your_layout_port); break; //---------------------------------------- case 2: //. SCREEN_ORIENTATION_REVERSE_PORTRAIT setContentView(R.layout.your_layout_port_rev); break; //---------------------------------------- case 1: //. SCREEN_ORIENTATION_LANDSCAPE setContentView(R.layout.your_layout_land); break; //---------------------------------------- case 3: //. SCREEN_ORIENTATION_REVERSE_LANDSCAPE setContentView(R.layout.your_layout_land_rev); break; //---------------------------------------- } /*endSwitch*/ rotationListener = new rotationListenerHelper(); rotationListener.listen(mContext, rotationCB); //... } private rotationCallbackFn rotationCB = new rotationCallbackFn() { @Override public void onRotationChanged(int lastRotation, int newRotation) { Log.d(TAG, "onRotationChanged: last " + (lastRotation) +" new " + (newRotation)); /** * no need to recreate activity if screen rotate from portrait to landscape * android do the job in order to reload resources */ if ( (lastRotation == 0 && newRotation == 2) || (lastRotation == 2 && newRotation == 0) || (lastRotation == 1 && newRotation == 3) || (lastRotation == 3 && newRotation == 1) ) ((Activity) mContext).recreate(); } }; /* destructor -----------------------------------------------------------------*/ @Override protected void onDestroy() { rotationListener.stop(); rotationListener = null; Log.i(TAG, "onDestroy: activity destroyed"); super.onDestroy(); } 

PASO FINAL: DISFRUTA

  • ¿Cómo hago girar la versión preliminar de vídeo de la versión de libstreaming?
  • La orientación de la pantalla de Android difiere entre los dispositivos
  • A veces no obtiene la devolución de llamada onCreateLoader después de llamar a initLoader
  • El diseño de la vista no se actualiza en el cambio de orientación en android
  • Bloquear la orientación del dispositivo - React Native (Android)
  • ¿La orientación de Android como dirección - rollo de tono de corrupción?
  • ¿Cómo se utilizan los valores de android.graphics.Matrix?
  • El diseño de Xml cambia al cambiar la orientación
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.