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.

Mi diseño XML:

 RelativeLayout (Parent) TextView FrameLayout/RelativeLayout <-- this need to be locked FrameLayout Button 

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.

  • Error de orientación de conmutación: Fragmento de error - ID de duplicado, etiqueta o identificador de origen 0x0
  • Android: ancho y alto de la vista después del cambio de orientación
  • Cómo detectar la orientación del fondo de pantalla en android
  • Android detecta si el video de la URL está en orientación vertical
  • ¿Hay alguna manera de recuperar datos de sensores múltiples en Android?
  • Bloquear la orientación de la pantalla (Android)
  • Widget de Android diferente orientación vertical y horizontal
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.