2016-04-09 1 views
5

Ich bin sehr neu für Android App Entwicklung, und versuche, das folgende Tastenlayout in Android Studio zu erreichen.Android Studio, wie man eine 2 Spalte LineareLayout

[App design[1]

Ive versucht, ein Linear-Layout zu verwenden, aber ich konnte es nicht richtig machen.

<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" 
    android:weightSum="1"> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="New Button" 
     android:id="@+id/button" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     android:background="#016eff" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_margin="10dp" 
     android:textColor="#ffffff" 
     android:layout_weight="0.48" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="New Button" 
     android:id="@+id/button2" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     android:background="#016eff" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_margin="10dp" 
     android:textColor="#ffffff" 
     android:layout_weight="0.48" /> 
</LinearLayout> 

Das Problem dabei ist, wenn ich eine andere Taste, um die Linear-Layout hinzugefügt, dann werden sie zusammen nur zerquetscht, sondern die Taste, um die nächste Zeile hinzuzufügen.

Kann mir bitte jemand zeigen, dass mein LinearLayout nur 2 Widgets in jeder Zeile hat oder einen anderen Fix liefert.

Jede Hilfe wird sehr geschätzt Dank :-)

+1

versuche mit tabellayout anstelle von linearlayout – Vishwa

+0

Okay, krank gehen und versuchen Sie es jetzt. –

+0

@Vishwa Vielen Dank, ich habe es geschafft zu arbeiten! –

Antwort

8

LinearLayout ist in Ordnung für das, was Sie erreichen möchten. Sehen Sie sich die Attribute für Gewicht und Ausrichtung eines LinearLayout-Objekts an. Linear Layout

Und was Sie wollen, können Sie wie folgt zum Beispiel tun:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:gravity="center"> 

     <TextView 
      android:text="Whatever You Want Here" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:textSize="36sp"/> 
    </LinearLayout> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:gravity="center"> 

      <Button 
       android:text="Button 1" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:gravity="center"/> 

      <Button 
       android:text="Button 2" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:gravity="center"/> 
     </LinearLayout> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:gravity="center"> 

      <Button 
       android:text="Button 3" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:gravity="center"/> 

      <Button 
       android:text="Button 4" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:gravity="center"/> 
     </LinearLayout> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:gravity="center"> 

      <Button 
       android:text="Button 5" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:gravity="center"/> 

      <Button 
       android:text="Button 6" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:gravity="center"/> 
     </LinearLayout> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:gravity="center"> 

      <Button 
       android:text="Button 7" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:gravity="center"/> 

      <Button 
       android:text="Button 8" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:gravity="center"/> 
     </LinearLayout> 
    </LinearLayout> 
</LinearLayout> 

Ausgang:

Linear Layout 2 Columns

Und achten, da nisten zu viele Gewicht Attribute einige negative haben Performance-Probleme.

1

Okay Jungs, ich es geschafft, ein fix dank Vishwa Kommentar zu finden. Allerdings habe ich keinen Weg gefunden, ein LinearLayout 2 Spalten zu haben.

Stattdessen wechselte ich zu einem TableLayout und streckte die Spalten 0 & 1, um den gesamten Bildschirm zu nehmen. Heres, wie mein XML endete. (Es hat extra Sachen in der es mein Design zu bekommen)

<TableLayout 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" 
    android:stretchColumns="0,1"> 

    <TableRow 
     android:layout_width="match_parent" 
     android:paddingBottom="8dp" 
     android:layout_height="match_parent"> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Events" 
      android:id="@+id/eventButton" 
      android:layout_column="0" 
      android:background="#016eff" 
      android:layout_marginRight="8dp" 
      android:textColor="#ffffff" 
      android:textStyle="normal" 
      android:textSize="40px" /> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Absentee" 
      android:id="@+id/absenteeButton" 
      android:layout_column="1" 
      android:background="#016eff" 
      android:textColor="#ffffff" 
      android:textStyle="normal" 
      android:textSize="40px" /> 

    </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:paddingBottom="8dp" 
     android:layout_height="match_parent" > 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Contacts" 
      android:id="@+id/contactsButton" 
      android:layout_column="0" 
      android:background="#016eff" 
      android:layout_marginRight="8dp" 
      android:textColor="#ffffff" 
      android:textStyle="normal" 
      android:textSize="40px" /> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Alerts" 
      android:id="@+id/alertButton" 
      android:layout_column="1" 
      android:background="#016eff" 
      android:textColor="#ffffff" 
      android:textStyle="normal" 
      android:textSize="40px" /> 
    </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:paddingBottom="8dp" 
     android:layout_height="match_parent" > 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Links" 
      android:id="@+id/linksButton" 
      android:layout_column="0" 
      android:background="#016eff" 
      android:layout_marginRight="8dp" 
      android:textColor="#ffffff" 
      android:textStyle="normal" 
      android:textSize="40px" /> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Newsletter" 
      android:id="@+id/newsletterButton" 
      android:layout_column="1" 
      android:background="#016eff" 
      android:textColor="#ffffff" 
      android:textSize="40px" 
      android:textStyle="normal" /> 
    </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:paddingBottom="8dp" 
     android:layout_height="match_parent" > 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Kamar" 
      android:id="@+id/kamarButton" 
      android:layout_column="0" 
      android:background="#016eff" 
      android:layout_marginRight="8dp" 
      android:textColor="#ffffff" 
      android:textStyle="normal" 
      android:textSize="40px" /> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="News" 
      android:id="@+id/newsButton" 
      android:layout_column="1" 
      android:background="#016eff" 
      android:textColor="#ffffff" 
      android:textSize="40px" 
      android:textStyle="normal" /> 
    </TableRow> 

</TableLayout> 
1

Sie legen sich lediglich über einen separaten Linearlayout mit android:orientation="horizontal" um jedes Paar von Tasten. Dann sollte das Parent LinearLayaout android:orientation="vertical" haben und das Gewicht sollte in jedem horizontalen LinearLayout sein.

+0

Danke für die Hilfe, es ist gut, mehrere Lösungen für ein Problem zu haben. –

+1

Ich finde die Verwendung von verschachtelten LinearLayouts gibt Ihnen mehr Flexibilität als ein TableLayout, wie wenn Sie eine Reihe von Schaltflächen benötigen, um anders auszukleiden, oder eine Zeile mit einer anderen Anzahl von Layouts haben. – Christine

Verwandte Themen