6

Estoy teniendo problemas reales tratando de alinear fragmentos con un RelativeLayout, aunque parece que esto debería ser sencillo. Sólo necesito dos fragmentos lado de la otra y si uso un LinearLayout funciona bien:Alineando fragmentos con RelativeLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

    <fragment android:name="com.fragment.test.TitlesFragment" 
      android:id="@+id/fragmentTitles" 
      android:layout_weight="1" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
    /> 

    <FrameLayout 
      android:id="@+id/details" 
      android:layout_weight="1" 
      android:layout_width="0px" 
      android:layout_height="fill_parent" 
    /> 

</LinearLayout> 

enter image description here

Sin embargo, si uso un RelativeLayout, no aparece nada:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

    <fragment android:name="com.fragment.test.TitlesFragment" 
      android:id="@+id/fragmentTitles" 
      android:layout_weight="1" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
    /> 

    <FrameLayout 
      android:id="@+id/details" 
      android:layout_weight="1" 
      android:layout_width="0px" 
      android:layout_height="fill_parent" 
      android:layout_toRightOf="@id/fragmentTitles"    
    /> 

</RelativeLayout> 

enter image description here

Actualización:

Aquí está una captura de pantalla de lo que estoy viendo:

enter image description here

Este es el código que estoy usando:

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_height="fill_parent" 
     android:layout_width="fill_parent" 
     android:weightSum="3" 
    > 
     <fragment android:name="com.fragment1" 
       android:id="@+id/fragment1" 
       android:layout_weight="1" 
       android:layout_width="0dp" 
       android:layout_height="fill_parent" 
       android:layout_above="@id/statusUpdated" 
     /> 

     <fragment android:name="com.fragment2" 
       android:id="@+id/fragment2" 
       android:layout_width="0px" 
       android:layout_weight="2" 
       android:layout_height="fill_parent" 
       android:layout_above="@id/statusUpdated"   
       android:layout_toRightOf="@id/fragment1" 
     /> 

    </LinearLayout> 

    <TextView android:id="@+id/statusUpdated" style="@style/Status" /> 

</RelativeLayout> 

Respuesta

5

No se puede usar pesas con una RelativeLayout. Básicamente, ese atributo se ignora, lo que significa que ambos fragmentos se procesan con un ancho de 0, por lo tanto, no son visibles.

No estoy seguro de lo que está tratando de lograr, pero es posible que desee considerar envolver su primer ejemplo (LinearLayout) en un RelativeLayout - en otras palabras: combine/integre ambos diseños. Sin embargo, eso da como resultado otro nivel en su jerarquía de vistas.


Editar:

que en realidad no lo he probado, pero creo que algo como esto es lo que está buscando:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <LinearLayout 
     android:id="@+id/fragment_layout" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_above="@+id/footer_view" 
     android:weightSum="3"> 

     <fragment 
      android:id="@+id/fragmentTitles" 
      android:name="com.fragment.test.TitlesFragment" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" /> 

     <FrameLayout 
      android:id="@+id/details" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="2" /> 
    </LinearLayout> 

    <TextView 
     android:id="@+id/footer_view" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:background="#f00" 
     android:text="I am a footer" /> 

</RelativeLayout> 

Obviamente se puede reemplazar el TextView con lo que quieras

+0

Lo que al final estoy tratando de hacer es agregar un pie de página en la parte inferior del diseño, pero no pude hacerlo funcionar con un 'LinearLayout'. Pude mostrar el pie de página usando un 'RelativeLayout', pero luego los fragmentos no aparecían, así que solo intentaba hacer aparecer los fragmentos (sin el pie de página), de ahí mi pregunta. Parece que voy a tener que descubrir cómo hacer que el pie de página funcione con un "LinearLayout". Básicamente, quiero un diseño donde el fragmento de la izquierda ocupa 1/3 de la pantalla, el derecho del resto de la pantalla, con un pie de página adhesivo. –

+0

Correcto, eso no debería ser demasiado difícil de realizar. Por favor, mira la edición en mi respuesta y pruébalo. No lo he probado, pero debería estar (cerca de) lo que está buscando. –

+0

Eso funcionó muy bien. ¡Gracias! –