6

Mam prawdziwe problemy z dopasowaniem fragmentów do RelativeLayout, chociaż wydaje się, że powinno to być proste. Muszę tylko dwa fragmenty obok siebie i jeśli mogę użyć LinearLayout to działa dobrze:Wyrównanie fragmentów za pomocą 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

Jednakże, jeśli mogę użyć RelativeLayout, nic nie pokazuje:

<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

Aktualizacja:

Oto zrzut ekranu z tego, co widzę:

enter image description here

Jest to kod używam:

<?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> 

Odpowiedz

5

Nie można używać ciężarów z RelativeLayout. Zasadniczo ten atrybut zostanie zignorowany, co oznacza, że ​​oba fragmenty będą renderowane o szerokości 0, a więc nie będą widoczne.

Nie jestem pewien, co próbujesz osiągnąć, ale możesz rozważyć zawijanie pierwszego przykładu (LinearLayout) do RelativeLayout - innymi słowy: łączenie/integrowanie obu układów. To jednak prowadzi do kolejnego poziomu w hierarchii widoku.


Edit:

nie zostały faktycznie przetestowane, ale liczyć się coś takiego, co szukasz:

<?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> 

Oczywiście można zastąpić TextView z wszystkim, co lubisz.

+0

Ostatecznie próbuję dodać stopkę do dolnej części układu, ale nie mogłem jej uruchomić za pomocą 'LinearLayout'. Udało mi się pokazać stopkę za pomocą "RelativeLayout", ale fragmenty się nie pojawiły, więc próbowałem tylko pokazać fragmenty (bez stopki), stąd moje pytanie. Wygląda na to, że muszę wymyślić, jak uruchomić stopkę za pomocą 'LinearLayout'. Zasadniczo chcę układu, w którym lewy fragment zajmuje 1/3 ekranu, a prawa reszta ekranu, z lepką stopką. –

+0

Dobrze, nie powinno to być zbyt trudne do zrealizowania. Zobacz edycję w mojej odpowiedzi i daj z siebie wszystko. Nie próbowałem tego, ale powinno być (blisko) tego, czego szukasz. –

+0

To działało świetnie. Dzięki! –

Powiązane problemy