2013-12-10 6 views
7

In der folgenden XML versuche ich ein Layout zu zeichnen, das zwei Blöcke (ein LinearLayout und ein TextView) enthält. Ich möchte, dass das LinearLayout 5 mal größer ist als das TextView. Dieses XML erzeugt genau das Gegenteil von dem, was ich erwartet habe, das TextView benötigt 5 mal mehr Speicherplatz als das LinearLayout. Beachten Sie, dass ich die Breite beider Elemente auf 0dp gesetzt habe, was ein übliches Versehen ist.layout_weight arbeitet gegenüber von dem, was es sollte

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingLeft="15dp" 
    android:paddingRight="15dp" 
    android:layout_gravity="center_vertical" 
    android:weightSum="6" 
    android:orientation="horizontal" > 

    <LinearLayout 
     android:layout_width="0dip" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:layout_weight="5" > 

     <TextView 
      android:id="@+id/result_title_textview" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:fontFamily="Arial" 
      android:textColor="#222222" 
      android:textSize="14sp"/> 

     <TextView 
      android:id="@+id/result_info_textview" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:fontFamily="Arial" 
      android:textColor="#777777" 
      android:textSize="12sp" /> 
    </LinearLayout> 

    <TextView 
     android:id="@+id/result_distance_textview" 
     android:layout_width="0dip" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:fontFamily="Arial" 
     android:textColor="#ffffffff" 
     android:textSize="14sp" /> 

</LinearLayout> 

EDIT: Bei diesem Layout ist eigentlich ein Listenelement, das in dieser Liste enthalten ist:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/results_list_fragment_layout" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:paddingTop="15dp" > 

    <ListView 
     android:id="@+id/search_results_list" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/scrollable_content_background" 
     android:divider="@drawable/listview_divider" 
     android:dividerHeight="1dp" > 
    </ListView> 

</LinearLayout> 
+1

set: android: layout_weight = "1" für lineares Layout und Android: layout_weight = "5" für Text. dh. Tausche dort Gewicht. – Cropper

+2

Ja, ich weiß, das funktioniert, aber die Frage ist mehr warum? es sollte das entgegengesetzte Verhalten haben. – TrtG

+1

Das ist .... falsch. Kannst du einen Screenshot hinzufügen? Wenn Sie kürzlich die Reihenfolge Ihrer Layouts geändert haben, versuchen Sie es mit einem sauberen Build. – Geobits

Antwort

4

Die Ausgabe aus der Containerliste kam, die eine „wrap_content“ Breite hatte. Der Wechsel zu "match_parent" hat das Problem behoben.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/results_list_fragment_layout" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:paddingTop="15dp" > 

    <ListView 
     android:id="@+id/search_results_list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/scrollable_content_background" 
     android:divider="@drawable/listview_divider" 
     android:dividerHeight="1dp" > 
    </ListView> 

</LinearLayout> 
+0

Das ist eine erstaunliche Entdeckung, vertrau mir! Obwohl es keinen Grund gibt, warum dies passiert ist, hat es mich zumindest dazu gebracht, den Punkt zu finden, wo es passiert ist. Ich habe ein Fragment in einem Frame-Layout der Aktivität aufgebläht. Mein Fragment war im 4: 6-Verhältnis, aber es verhielt sich wie 6: 4, weil die Rahmenlayout-Innenaktivität ** android: layout_height = "wrap_content" ** hatte. Ändern, was zu Match_parent hat den Trick! – sud007

5

traf ich auch nur das gleiche Problem, und die Art und Weise zu lösen, ist zu ändern:

  • Breite von Kind zu 0DP Ansicht, wenn ein Elternteil LinearLayoutorientation="horizontal" hat;

  • Höhe der Kinder Blick auf 0DP wenn Eltern LinearLayoutorientation="vertical" hat.

+1

Das hat mir geholfen, weil ich in meinem Beispiel das Layout nicht auf match_parent ändern wollte. Vielen Dank. Vielleicht sollte das die richtige Antwort sein. –

Verwandte Themen