2011-01-13 4 views
6

Ich möchte ein Layout erstellen, mit einem horizontalen LinearLayout (s) oben und unten, ein ListView füllen in der Mitte.Layout-Problem: Wie platziert man etwas oben und unten?

Wie kann ich die main.xml definieren?

Ich habe versucht, ein Layout mit horizontalen LinearLayout oben, TextView auf der Unterseite, eine ListView ausfüllen in der Mitte; ist in Ordnung. Aber nachdem ich den unteren TextView auf LinearLayout geändert habe, verschwindet der untere LinearLayout.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     > 
     <TextView 
      android:textSize="12px" 
      android:text="something here" 
      android:layout_width="50px" 
      android:layout_height="wrap_content" 
      /> 
     <TextView 
      android:textSize="12px" 
      android:text="something here" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      /> 
    </LinearLayout> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:gravity="bottom" 
     > 
     <ListView 
      android:id="@+id/listbody" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      /> 
     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_height="wrap_content" 
      android:layout_width="fill_parent" 
      > 
      <TextView 
       android:layout_height="wrap_content" 
       android:layout_width="0dip" 
       android:layout_weight="1" 
       android:textSize="12px" 
       android:text="50%" 
       /> 
      <TextView 
       android:layout_height="wrap_content" 
       android:layout_width="0dip" 
       android:layout_weight="1" 
       android:textSize="12px" 
       android:text="50%" 
       /> 
     </LinearLayout> 
    </LinearLayout> 
</LinearLayout> 

Kann jemand beraten? Bitte helfen.

Antwort

11

Versuchen Sie, die ganze Reihe in einem RelativeLayout enthält:

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

    <LinearLayout 
     android:id="@+id/top_linear_layout_id" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" > 
    </LinearLayout> 

    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/bottom_linear_layout_id" 
     android:layout_below="@id/top_linear_layout_id" /> 

    <LinearLayout 
     android:id="@+id/bottom_linear_layout_id" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" > 
    </LinearLayout> 
</RelativeLayout> 

Herausgegeben mit Schlichte Sachen.

+0

Nun überlappt das LinearLayout mit dem ListView! Ich denke, bei der Definition des LinearLayouts unten sollte etwas falsch sein. – chow

+0

Nicht besonders. Für die Listview hinzu: \t android: layout_height = "wrap_content" \t android: layout_width = "fill_parent" \t android: layout_below = "@ id/top_linear_layout_id" \t android: layout_above = "@ + id/bottom_linear_layout_id" Zusammen mit den jeweiligen IDs für die linearen Layouts. – Estel

0

Das Problem wäre, dass Sie beide in Bezug auf die Größe des Inhalts definieren, aber wenn der kombinierte Inhalt von beiden größer als der Bildschirm ist, dann muss einer von ihnen den anderen, Sie überlappen wäre besser, die layout_height mit einer absoluten Messung in einem linearen Layout zu definieren

3

Hier ist, was Sie suchen. Estel ist auf der richtigen Spur mit dem RelativeLayout (obwohl es mit einem LinearLayout gemacht werden kann, denke ich, der RelativeLayout-Ansatz ist sauberer) aber das Layout ist außer Betrieb Nevermind, überprüfen Sie Estel's Kommentar, der mir falsch war. :) Du solltest zuerst deine Kopfzeile definieren, sie nach oben ausrichten; Als nächstes definieren Sie Ihre Fußzeile und richten Sie sie nach unten aus; schließlich erklären, um Ihre Listview, geben Sie ihm eine Höhe von fill_parent, und legen Sie es über der Fußzeile und unter dem Header Layout:

<?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/header" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:alignParentTop="true" 
     > 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/footer" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:alignParentBottom="true" 
     > 
    </LinearLayout> 

    <ListView 
     android:id="@+id/listview" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_above="@id/footer" 
     android:layout_below="@id/header" 
     /> 
</RelativeLayout> 
+0

Aus Interesse, wie wichtig ist die Bestellung in diesem Fall? Während wir explizit sicherstellen, dass es bei der Positionierung eines Widgets keine Überlappung gibt, wird die Zeichnungsreihenfolge nicht immer überflüssig? – Estel

+0

Lesen Sie hier: http://developer.android.com/guide/topics/ui/index.html#ViewHierarchy es besagt, dass das Layout in der Reihenfolge analysiert wird. Wenn also ListView vor dem Footer-Layout deklariert wird, benötigt es den gesamten Platz von unterhalb der Kopfzeile bis zum unteren Bildschirmrand. Dann würde die Fußzeile über die ListView gezeichnet werden, da die ListView die Fußzeile zum Zeitpunkt der Analyse nicht kennt. Ich möchte sagen, dass ich das von Android 2 gelesen habe.2, die Reihenfolge ist nicht mehr wichtig (aber immer noch, wenn Sie auf frühere Versionen zielen), aber ich kann jetzt keine definitive Referenz finden. – kcoppock

+0

Eine Sache, die ich http://developer.android.com/resources/articles/ui-1.6.html gefunden habe, die relevant sein kann, ist, dass 1.6 vorwärts gerichtete Verweise auf Ressourcen aktiviert hat, die weiter unten im Baum deklariert werden. Ist das was du meinst? – Estel

0

Dank allen für Ihre Beratung und Richtung RelativeLayout. Ich kann es jetzt reparieren.

Ich habe die Fußzeile Linearlayout vor dem Listview zu definieren, und definieren Listview als android: layout_alignParentTop = "true" und Android: layout_above = "@ id/Fußzeile"

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
    > 
     <TextView 
      android:textSize="12px" 
      android:text="something here" 
      android:layout_width="50px" 
      android:layout_height="wrap_content" 
      /> 
     <TextView 
      android:textSize="12px" 
      android:text="something here" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
     /> 
    </LinearLayout> 
    <RelativeLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
    > 
     <LinearLayout 
      android:id="@+id/footer" 
      android:orientation="horizontal" 
      android:layout_height="wrap_content" 
      android:layout_width="fill_parent" 
      android:layout_alignParentBottom="true" 
     > 

      <TextView 
       android:layout_height="wrap_content" 
       android:layout_width="0dip" 
       android:layout_weight="1" 
       android:textSize="12px" 
       android:text="50%" 
       /> 
      <TextView 
       android:layout_height="wrap_content" 
       android:layout_width="0dip" 
       android:layout_weight="1" 
       android:textSize="12px" 
       android:text="50%" 
      /> 

     </LinearLayout> 
     <ListView 
      android:id="@+id/tablebody" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_above="@id/footer" 
     /> 
    </RelativeLayout> 
</LinearLayout> 
0

Dies wird definitiv lösen Ihr Problem :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <HorizontalScrollView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 
     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 
      <TextView 
       android:textSize="12sp" 
       android:text="something here" 
       android:layout_width="50dp" 
       android:layout_height="wrap_content"/> 
      <TextView 
       android:textSize="12sp" 
       android:text="something here" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content"/> 
     </LinearLayout> 
    </HorizontalScrollView> 
    <ListView 
     android:id="@+id/listbody" 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"/> 
    <HorizontalScrollView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 
     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_height="wrap_content" 
      android:layout_width="fill_parent"> 
      <TextView 
       android:layout_height="wrap_content" 
       android:layout_width="0dip" 
       android:layout_weight="1" 
       android:textSize="12sp" 
       android:text="50%"/> 
      <TextView 
       android:layout_height="wrap_content" 
       android:layout_width="0dip" 
       android:layout_weight="1" 
       android:textSize="12sp" 
       android:text="50%"/> 
     </LinearLayout> 
    </HorizontalScrollView> 
</LinearLayout> 
Verwandte Themen