2012-05-25 16 views
15

Chcę utworzyć wymienia dwie kolumny formularza wejściowego takiego:Jak utworzyć zgrabny formularz wprowadzania dwóch kolumn w systemie Android?

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

Moje xml kodu układ do tej pory:

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

Widać, że użyłem LinearLayout wykorzystując layout_weight & layout_width = "0dp", aby podział dwóch kolumn wyglądał zadowalająco. W kodzie HTML możemy użyć% szerokości. Ale w układzie androida xml nie ma% -wartości dla layout_width. Chcę uniknąć zakodowanych wartości dp dla szerokości kolumny. Czy to możliwe w Androidzie?

Jak na razie to najlepsze, co mogę zrobić. Teraz chcę zmienić rozmiar pola tekstowego ISBN, aby stał się mniejszy (50% mniej od bieżącego rozmiaru). Ale nie mogę zmienić rozmiaru szerokości pola tekstowego, ponieważ layout_width musi mieć wartość "0dp", aby układ_waga działała. Ja też chcę zrobić to samo z przyciskiem Zatwierdź i Anuluj.

Zastanawiam się, czy korzystanie z LinearLayout jest poprawnym sposobem tworzenia formularzy wejściowych w systemie Android. Czy TableLayout może być lepszy? Słyszałem, że nie można zmienić wielkości layout_width widoku podrzędnego w TableLayout.

Jest kilka osób, które polecają mi używanie RelativeLayout, próbowałem go, ale nie obsługuje on layout_weight.

+1

Z powodu tego powodu używa względna Układ zapewnia maksymalną elastyczność i można zmienić pojedynczy widok w zależności od potrzeb .... –

+0

Proponuję użyć względna Układ, ponieważ zapewnia większą elastyczność. –

+0

@both of you above: staraj się dać przykład? – null

Odpowiedz

22

Dobra, znalazłem się tak z LinearLayout. Sztuczka polega na zawijaniu widoków, które chcesz zmienić za pomocą LinearLayout. Następnie szerokość samego widoku można dostosować za pomocą opcji: android: layout_width lub android: ems.

Oto kod xml:

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

dziękuję za to !!!!!!! – user2202911

2

powinieneś także rozważyć gridLayout, który może być użyty (przy użyciu biblioteki android) na API7 +.

oto link: http://android-developers.blogspot.com/2011/11/new-layout-widgets-space-and-gridlayout.html

+0

Nie sądzę, żebym mógł go użyć. Celuję minimalnie na Androida v2.2 (API 8+). – null

+0

8 jest większy niż 7, więc powinien działać również dla Ciebie. :) –

+0

Jesteś pewien? Kiedy kliknąłem podany link do gry, otrzymałem komunikat ostrzegawczy "Ta klasa nie jest dostępna z API poziomu 8. Aby korzystać z tej klasy, aplikacja musi określać poziom API" 14 "lub wyższy" – null

Powiązane problemy