Cómo evitar el cambio de orientación para un diseño, no toda la pantalla / actividad
Necesitaría un diseño de niño (puede ser cualquier diseño, como FrameLayout
o RelativeLayout
) para ignorar el cambio de orientación y permanecer siempre en la orientación horizontal. Pero no su padre o cualquier otro hermano diseños / vistas, deben cambiar su orientación en consecuencia.
Por lo tanto, no puedo usar setRequestedOrientation()
o android:screenOrientation="landscape"
. O bloqueará la orientación para toda la pantalla o la actividad. Necesito bloquear solo este solo diseño.
- Diferentes valores entre los sensores TYPE_ACCELEROMETER / TYPE_MAGNETIC_FIELD y TYPE_ORIENTATION
- Android: Obtener orientación del dispositivo actual
- ¿Cómo puedo bloquear el diseño de mi programa Android a una orientación
- ¿Cómo configurar el android para mostrar la orientación vertical?
- Android SensorManager extraño cómo remapCoordinateSystem
Mi diseño XML:
RelativeLayout (Parent) TextView FrameLayout/RelativeLayout <-- this need to be locked FrameLayout Button
- Evitar que se reinicie la aplicación Android de paisaje en el bloqueo
- Evitar el cambio de orientación de Android en ciertos dispositivos
- ¿Cómo obtener orientación inicial para los cálculos con giroscopio?
- Manejar estados de cambios de orientación
- Descripción de la orientación del teléfono Android, incluida la brújula
- Vídeo de Android reiniciado con orientación
- Cómo seguir reproduciendo video mientras cambia a modo horizontal android
- ¿Cómo puedo configurar Orientación fija para todas las actividades
Probablemente depende de lo que quiere decir "no cambiar de orientación". Sin embargo, creo que el mejor lugar para comenzar sería crear su propia clase para la parte que no debe cambiar. Así que el layout xml ahora tiene dos archivos:
Main_layout.xml
RelativeLayout (Parent) TextView MyNonChangingLayout
My_non_changing_layout.xml
RelativeLayout FrameLayout Button
Donde has creado
MyNonChangingLayout extends FrameLayout { MyNonchangingLayout(Context content) { super(context); myContext = context; makeFromXML(); } private void makeFromXML() { LayoutInflater inflater = (LayoutInflater)myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); topView = inflater.inflate(MyR.layout.my_non_changing_layout, this, false); // Get all the sub Views here using topView.findViewById() // Do any other initiation of the View you need here // Make sure you this otherwise it won't actually appear! super.addView(topView); } /* * Then, you can override quite a lot of the layout's calls and * enforce behaviour on the children. Two examples: */ // To specifically catch orientation changes @Overridge onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); // You could create the layout here by removing all views and rebuilding them // Perhaps by having a two xml layouts, one of which is "90 degrees out" ... // If you do make the layot here, make sure you don't clash with the constructor code! switch (newConfig.orientation) { case ORIENTATION_LANDSCAPE: // Make the layout for this orientation (as per above) break; case ORIENTATION_PORTRAIT: // Make the layout for this orientation (as per above) break; case ORIENTATION_SQUARE: // Make the layout for this orientation (as per above) break; } } //to handle size changes to enforce aspect ratios on children: @override protected void onSizeChanged (int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); int viewWidth = //something I've determine int viewHeight = //something I've determined setViewsize(viewToHaveSizedControlled, viewWidth, viewheight); } // The post thing means that it doesn't crash no matter which thread it is // executed on ... private void setViewsize(final View v, final int w, final int h) { post(new Runnable() { public void run() { ViewGroup.LayoutParams lp = v.getLayoutParams(); lp.width = w; lp.height = h; v.setLayoutParams(lp); }}); }
}
A continuación, puede aplicar muy bien todo lo que desee. Si puede ser más específico sobre qué comportamiento desea aplicar en la subregión, podría sugerir un código más específico.
Una cosa que usted puede querer hacer es mantener
Amplíe la clase de diseño que desea mantener la orientación vertical y anule el método dispatchConfiurationChanged (Configuration) de la siguiente manera.
public void dispatchConfigurationChanged(Configuration newConfig) { Configuration c = new Configuration(newConfig); // copy c.orientation = ORIENTATION_PORTRAIT; // lock to portrait super.dispatchConfigurationChanged(c); }
Asegúrese de que su aplicación está configurada para no reiniciar los cambios de orientación.