6

Ich habe echte Probleme beim Ausrichten von Fragmenten mit einer RelativeLayout, obwohl es scheint, sollte dies einfach sein. Ich brauche nur zwei Fragmente nebeneinander, und wenn ich eine LinearLayout verwenden, es funktioniert:Ausrichten von Fragmenten mit 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

Wenn ich jedoch eine RelativeLayout verwenden, nichts zeigt:

<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

Aktualisierung:

Hier ist ein Screenshot von dem, was ich sehe:

enter image description here

Dies ist der Code Ich verwende:

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

Antwort

5

Sie nicht Gewichte mit einem RelativeLayout verwenden können. Grundsätzlich wird dieses Attribut ignoriert, was bedeutet, dass beide Fragmente mit einer Breite von 0 gerendert werden und daher nicht sichtbar sind.

Ich bin mir nicht sicher, was Sie erreichen möchten, aber Sie könnten in Betracht ziehen, Ihr erstes Beispiel (LinearLayout) in eine RelativeLayout zu verpacken - mit anderen Worten: beide Layouts kombinieren/integrieren. Das führt jedoch zu einer anderen Ebene in Ihrer Ansichtshierarchie.


Edit:

Ich habe es tatsächlich nicht getestet, aber ich denke, so etwas wie das ist, was Sie suchen:

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

Offensichtlich können Sie die TextView ersetzen mit allem, was du magst.

+0

Was ich letztendlich versuche zu tun ist eine Fußzeile am unteren Rand des Layouts hinzuzufügen, aber ich konnte es nicht mit einem 'LinearLayout' arbeiten. Ich konnte die Fußzeile mit einem 'RelativeLayout' zeigen, aber dann erschienen die Fragmente nicht, also versuchte ich nur die Fragmente zu bekommen (ohne Fußzeile), daher meine Frage. Klingt so, als müsste ich herausfinden, wie man die Fußzeile mit einem 'LinearLayout' zur Arbeit bringt. Ich möchte grundsätzlich ein Layout, bei dem das linke Fragment 1/3 des Bildschirms einnimmt, das rechte den Rest des Bildschirms, mit einer klebrigen Fußzeile. –

+0

Richtig, das sollte nicht zu schwer zu realisieren sein. Bitte beachten Sie die Bearbeitung in meiner Antwort und geben Sie es an. Habe es nicht ausprobiert, aber es sollte (nahe) sein, wonach du suchst. –

+0

Das hat super funktioniert. Vielen Dank! –