2010-11-25 8 views
8

Wie kann ich diese Schaltflächen auf Android zentrieren? alt textCenter Layout Android

Der Code, den ich in der layout verwendet, ist:

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

     <TextView android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/hello" /> 

     <LinearLayout android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="horizontal"> 

      <Button android:text="Test" 
       android:layout_width="100px" 
       android:layout_height="40px" /> 
      <Button android:text="Test" 
       android:layout_width="100px" 
       android:layout_height="40px" /> 
      <Button android:text="Test" 
       android:layout_width="100px" 
       android:layout_height="40px" /> 
      <Button android:text="Test" 
       android:layout_width="100px" 
       android:layout_height="40px" /> 
     </LinearLayout> 
</LinearLayout> 

Antwort

15

Machen Sie das LinearLayout, das die Schaltflächen enthält, um wrap_content auf width zu setzen und center_horizontal auf der Schwerkraft zu haben.

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

<TextView android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/hello" /> 

<LinearLayout android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:orientation="horizontal" 
     android:layout_gravity="center_horizontal"> 
    <Button android:text="Test" 
     android:layout_width="100px" 
     android:layout_height="40px" /> 
    <Button android:text="Test" 
     android:layout_width="100px" 
     android:layout_height="40px" /> 
    <Button android:text="Test" 
     android:layout_width="100px" 
     android:layout_height="40px" /> 
    <Button android:text="Test" 
     android:layout_width="100px" 
     android:layout_height="40px" /> 
     </LinearLayout> 
</LinearLayout> 
+1

Ausgezeichnet. Vielen Dank! –

+0

Perfekt arbeiten, danke. –

1

Ich würde gerne wissen, ob jemand irgendwelche Kommentare zu diesem Thema hat auch. Ich habe es immer mit einem TableLayout getan und sowohl stretchColumns als auch collapseColumns für alle Spalten gesetzt, sodass es auf allen Bildschirmgrößen skaliert werden kann. Ein wenig wie das:

Ich frage mich, ob es der beste Weg ist.