2012-05-25 11 views
15

Ich mag ein sauberes Zwei Spalten Eingabeformular wie folgt erstellen:Wie erstelle ich ein nettes zweispaltiges Eingabeformular in Android?

Code

http://img31.imageshack.us/img31/115/addnew2.png

Mein xml Layout so weit: können

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

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

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.25" 
      android:text="@string/lblTitle" /> 

     <EditText 
      android:id="@+id/txtTitle" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.75" 
      android:ems="10" /> 
    </LinearLayout> 

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

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.25" 
      android:text="@string/lblAuthor" /> 

     <EditText 
      android:id="@+id/txtAuthor" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.75" 
      android:ems="10" /> 
    </LinearLayout> 

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

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.25" 
      android:text="@string/lblPublisher" /> 

     <EditText 
      android:id="@+id/txtPublisher" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.75" 
      android:ems="10" /> 
    </LinearLayout> 

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

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.25" 
      android:text="@string/lblIsbn" /> 

     <EditText 
      android:id="@+id/txtIsbn" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.75" 
      android:ems="10" /> 
    </LinearLayout> 

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

     <View 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.25" /> 

     <Button 
      android:id="@+id/btnSubmit" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.375" 
      android:text="@string/submit" /> 

     <Button 
      android:id="@+id/btnCancel" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.375" 
      android:text="@string/cancel" /> 
    </LinearLayout> 

</LinearLayout> 

Sie sehen, dass ich Linearlayout verwendet verwendet layout_weight & layout_width = "0dp" Trick, um die zwei Spalten Division ordentlich aussehen zu lassen. Im HTML-Code können wir mit% width size verwenden. Aber im android XML-Layout gibt es keinen% -Wert für layout_width. Ich möchte hartcodierte dp-Werte für die Spaltenbreite vermeiden. Ist es bei Android möglich?

Bis jetzt ist das das Beste, was ich tun kann. Jetzt möchte ich die Größe der ISBN-Textbox verkleinern (50% kleiner als die aktuelle Größe). Aber ich kann die Breite des Textfelds nicht ändern, da layout_width "0dp" sein muss, damit layout_weight funktioniert. Ich möchte auch das gleiche mit der Größe der Schaltfläche Submit und Cancel tun.

Ich frage mich, ob die Verwendung von LinearLayout eine korrekte Methode ist, um Eingabeformular ordentlich in Android zu erstellen. Könnte TableLayout dafür besser sein? Ich habe gehört, dass Sie in layout_widget nicht die layout_width der Kindansicht ändern können.

Es gibt einige Leute hier, die mir empfehlen, RelativeLayout zu verwenden, ich habe es versucht, aber es unterstützt nicht layout_weight.

+1

Aus diesem Grunde i Relative Layout verwendet es ein Maximum an Flexibilität bietet und Sie können eine einzelne Ansicht nach Ihrem Bedarf ändern .... –

+0

Ich würde vorschlagen, Relativ zu verwenden Layout, weil es mehr Flexibilität bietet. –

+0

@both von Ihnen oben: geben Sie ein Beispiel? – null

Antwort

22

Okay, fand ich mit Linearlayout den Weg aus. Der Trick besteht darin, die Ansichten, die Sie mit LinearLayout in der Größe ändern möchten, zu umbrechen. Dann kann die Breite von View selbst angepasst werden mit: Android: layout_width oder android: ems property.

Hier ist der XML-Code:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

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

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.25" 
      android:text="@string/lblTitle" /> 

     <EditText 
      android:id="@+id/txtTitle" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.75" 
      android:ems="10" /> 
    </LinearLayout> 

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

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.25" 
      android:text="@string/lblAuthor" /> 

     <EditText 
      android:id="@+id/txtAuthor" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.75" 
      android:ems="10" /> 
    </LinearLayout> 

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

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.25" 
      android:text="@string/lblPublisher" /> 

     <EditText 
      android:id="@+id/txtPublisher" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.75" 
      android:ems="10" /> 
    </LinearLayout> 

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

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:layout_weight="0.25" 
      android:text="@string/lblIsbn" /> 

     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.75" 
      android:orientation="horizontal" > 

      <EditText 
       android:id="@+id/txtIsbn" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:ems="5" /> 
     </LinearLayout> 
    </LinearLayout> 

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

     <View 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.25" /> 

     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.75" 
      android:orientation="horizontal" > 

      <Button 
       android:id="@+id/btnSubmit" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:ems="5" 
       android:text="@string/submit" /> 

      <Button 
       android:id="@+id/btnCancel" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:ems="5" 
       android:text="@string/cancel" /> 
     </LinearLayout> 
    </LinearLayout> 

</LinearLayout> 
+1

danke für diese !!!!!!! – user2202911

2

sollten Sie auch die gridLayout betrachten, die (mit einer Android-Bibliothek) auf API7 + verwendet werden kann.

hier ein Link: http://android-developers.blogspot.com/2011/11/new-layout-widgets-space-and-gridlayout.html

+0

Ich glaube nicht, dass ich es benutzen kann. Ich ziele minimal auf Android v2.2 (API 8+). – null

+0

8 ist größer als 7, also sollte es auch für Sie funktionieren. :) –

+0

Bist du sicher? Als ich auf den Gitter-Link geklickt habe, bekam ich die Warnmeldung 'Diese Klasse ist nicht verfügbar mit API Level 8. Um diese Klasse zu verwenden, muss Ihre Anwendung API Level "14" oder höher' – null

Verwandte Themen