2013-06-04 5 views
5

Ich habe ein Layout wie im Taschenrechner, 4 Tasten in jeder Zeile, mit etwa 5 Zeilen. Ich habe diese Zeilen mit LinearLayout erstellt. Ich hatte es geschafft, die Breite jeder Reihe vollständig zu füllen, indem ich android:layout_weight="1" in jeder Taste in LinearLayout verwendete. Aber ich weiß nicht, wie man den vertikalen Raum auf diese Weise füllt. Nach fünf Reihen an der Unterseite gibt es Platz für normale XDPI Bildschirme verbleibenden (Nexus 4)Wie gleichmäßig Schaltflächen und Layouts vertikal in Android?

<LinearLayout 
     android:id="@+id/first_row" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/entry"> 

    <Button 
      android:id="@+id/seven" 
      android:layout_width="0dip" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:text="7" 
      android:textColor="@color/Blue_ICS" 
      android:textSize="@dimen/button_text_size" 
      /> 

    <Button 
      android:id="@+id/eight" 
      android:layout_width="0dip" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:text="8" 
      android:textColor="@color/Blue_ICS" 

      android:textSize="@dimen/button_text_size"/> 

    <Button 
      android:id="@+id/nine" 
      android:layout_width="0dip" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:text="9" 

      android:textColor="@color/Blue_ICS" 
      android:textSize="@dimen/button_text_size"/> 

    <Button 
      android:id="@+id/plus" 
      android:layout_width="0dip" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:text="+" 
      android:textColor="@color/Blue_ICS" 
      android:textSize="@dimen/button_text_size"/> 
</LinearLayout> 

Das Layout ist zu lang ich hier so teile Code von nur ersten Zeilen sind restliche Zeilen zu gleich.

+0

Möchten Sie Ihre Zeilen mit gleicher Höhe füllen? –

+0

Haben Sie versucht, das 'LinearLayout' Höhenattribut zu ändern ?:' android: layout_height = "match_parent" ' –

+0

überprüfen Sie [diesen Link] (http://stackoverflow.com/a/8105131/2345913) – CRUSADER

Antwort

7

Sie einen TableLayout verwenden sollten, werden alle Ihre Linearlayout für TableRow ersetzen. Mit dem Attribut stretchColumns können Sie alle Spalten mit der gleichen Breite erstellen, ohne Gewicht zu verwenden. Auf diese Weise können Sie das Gewicht verwenden, um mit der TableRow-Höhe umzugehen.

Im Beispiel unten habe ich sogar etwas Platz (leeres LinearLayout) hinzugefügt, um die Ergebnisse anzuzeigen, wie ein Taschenrechnerbildschirm. Ich hoffe, das ist was du suchst! (Hier finden Sie die IDs ändern müssen)

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

<LinearLayout 
    android:id="@+id/screenLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="150dp" 
    android:orientation="vertical"></LinearLayout> 

<TableLayout 
    android:id="@+id/myLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_below="@+id/screenLayout" 
    android:stretchColumns="*" > 

    <TableRow 
     android:id="@+id/first_row" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" > 

     <Button 
      android:id="@+id/seven" 
      android:layout_width="0dip" 
      android:layout_height="fill_parent" 
      android:text="7" /> 

     <Button 
      android:id="@+id/eight" 
      android:layout_width="0dip" 
      android:layout_height="fill_parent" 
      android:text="8" /> 

     <Button 
      android:id="@+id/nine" 
      android:layout_width="0dip" 
      android:layout_height="fill_parent" 
      android:text="9" /> 

     <Button 
      android:id="@+id/plus" 
      android:layout_width="0dip" 
      android:layout_height="fill_parent" 
      android:text="+" /> 
    </TableRow> 

    <TableRow 
     android:id="@+id/first_row" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" > 

     <Button 
      android:id="@+id/seven" 
      android:layout_width="0dip" 
      android:layout_height="fill_parent" 
      android:text="4" /> 

     <Button 
      android:id="@+id/eight" 
      android:layout_width="0dip" 
      android:layout_height="fill_parent" 
      android:text="5" /> 

     <Button 
      android:id="@+id/nine" 
      android:layout_width="0dip" 
      android:layout_height="fill_parent" 
      android:text="6" /> 

     <Button 
      android:id="@+id/plus" 
      android:layout_width="0dip" 
      android:layout_height="fill_parent" 
      android:text="-" /> 
    </TableRow> 

    <TableRow 
     android:id="@+id/first_row" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" > 

     <Button 
      android:id="@+id/seven" 
      android:layout_width="0dip" 
      android:layout_height="fill_parent" 
      android:text="1" /> 

     <Button 
      android:id="@+id/eight" 
      android:layout_width="0dip" 
      android:layout_height="fill_parent" 
      android:text="2" /> 

     <Button 
      android:id="@+id/nine" 
      android:layout_width="0dip" 
      android:layout_height="fill_parent" 
      android:text="3" /> 

     <Button 
      android:id="@+id/plus" 
      android:layout_width="0dip" 
      android:layout_height="fill_parent" 
      android:text="*" /> 
    </TableRow> 

    <TableRow 
     android:id="@+id/first_row" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" > 

     <Button 
      android:id="@+id/seven" 
      android:layout_width="0dip" 
      android:layout_height="fill_parent" 
      android:text="0" /> 

     <Button 
      android:id="@+id/eight" 
      android:layout_width="0dip" 
      android:layout_height="fill_parent" 
      android:text="," /> 

     <Button 
      android:id="@+id/nine" 
      android:layout_width="0dip" 
      android:layout_height="fill_parent" 
      android:text="/" /> 

     <Button 
      android:id="@+id/plus" 
      android:layout_width="0dip" 
      android:layout_height="fill_parent" 
      android:text="=" /> 
    </TableRow> 
</TableLayout> 

</RelativeLayout> 

Ergebnis:

enter image description here

+0

Ja, das wird helfen, das gesamte Layout gleichmäßig zu überspannen. Danke – Vins

+0

Froh, zu helfen! :) –

2

Wenn Ihre Eltern auch ein LinearLayout ist, können Sie Benutzer die android:layout_weight="1" auf zu Ihren Reihen.

<!--parent--> 
<LinearLayout 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:orientation="vertical"> 

    <include 
     android:layout="@layout/myrow" 
     android:layout_height="0dp" 
     android:layout_width="match_parent" 
     android:layout_weight="1"/> 

    <include 
     android:layout="@layout/myrow" 
     android:layout_height="0dp" 
     android:layout_width="match_parent" 
     android:layout_weight="1"/> 

    <include 
     android:layout="@layout/myrow" 
     android:layout_height="0dp" 
     android:layout_width="match_parent" 
     android:layout_weight="1"/> 

    <include 
     android:layout="@layout/myrow" 
     android:layout_height="0dp" 
     android:layout_width="match_parent" 
     android:layout_weight="1"/> 

    <include 
     android:layout="@layout/myrow" 
     android:layout_height="0dp" 
     android:layout_width="match_parent" 
     android:layout_weight="1"/> 


</LinearLayout> 

Habe es nicht getestet, aber es sollte funktionieren.

this helps

0

try this:

<LinearLayout 
     android:id="@+id/first_row" 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"> 
     ... 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/first_row" 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"> 
     ... 
    </LinearLayout> 
    ... 

und so weiter. Wickeln Sie alle diese in lineares Layout mit vertikaler Ausrichtung

0

Sie können Folgendes versuchen ... (PseudoCode) :(hoffe, Sie verstehen die Logik ... Dies funktioniert .. es dehnt den Knopf auf jedem Bildschirm Dichte)

 <linearlayout=Outer 
      horizontal 
      weightSum=5> 
       <sub linearLayout=innerRow1 
        vertical 
        weightSum=1> 
        <enter all the buttons with weight as ".25"> 
       </sub innerRow1> 
       <sub linearLayout=innerRow2 
        vertical 
        weightSum=1> 
        <enter all the buttons with weight as ".25"> 
       </sub innerRow2> 
       <sub linearLayout=innerRow3 
        vertical 
        weightSum=1> 
        <enter all the buttons with weight as ".25"> 
       </sub innerRow3> 
      <sub linearLayout=innerRow4 
        vertical 
        weightSum=1> 
        <enter all the buttons with weight as ".25"> 
       </sub innerRow4> 
      <sub linearLayout=innerRow5 
        vertical 
        weightSum=1> 
        <enter all the buttons with weight as ".25"> 
       </sub innerRow5> 
+0

Mein Root-Layout ist RelativeLayout, also kann ich Gewichtssumme nicht verwenden, denke ich – Vins

+0

Sie können es mit layout_weight versuchen oder Sie können dieses lineare Layout in Ihrem RelativeLayout @vins einschließen ... – amalBit

Verwandte Themen