2009-07-24 16 views
69

Chciałbym utworzyć okno dialogowe wyświetlające tytuł wideo i znaczniki. Poniżej tekstu chciałbym dodać przyciski Przeglądaj, Edytuj i Usuń i uczynić te elementy takim samym rozmiarem. Czy ktokolwiek wie, jak zmodyfikować plik układu .xml, aby elementy wewnątrz LinearView były tego samego rozmiaru?Android: jak zrobić wszystkie elementy wewnątrz LinearLayout tego samego rozmiaru?

Bieżący plik układ wygląda następująco:

<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="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/txtTitle" android:text="[Title]" > 
      </TextView> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/txtTags"    
       android:text="[Tags]" > 
      </TextView> 

    </LinearLayout> 

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

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/btnPlay" 
      android:text="View"> 
     </Button> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/btnEdit" 
      android:text="Edit"> 
     </Button> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/btnDelete" 
      android:text="Delete"> 
     </Button> 

    </LinearLayout> 

</LinearLayout> 

Będę wdzięczny jeśli ktoś może pokazać rozwiązania, modyfikując zawartość pliku wklejone.

Dzięki!

Odpowiedz

148

Użyj android:layout_width="0px" i android:layout_weight="1" dla trzech Button s. Oznacza to, że przyciski powinny zajmować nie więcej niż 0 pikseli, ale cała trójka powinna podzielić między nimi dodatkową przestrzeń. To powinno dać Ci efekt wizualny.

+4

Jeśli TYLKO chciałeś, możesz alternatywnie użyć TableLayout z Androidem: stretchColumns = "0,1,2". –

+0

Awesome ... więc to oznacza, że ​​jeśli ustawimy szerokość na 0px, waga zastąpi ustawienia? Dlaczego tak jest? – noob

+0

To działało idealnie. – Proverbio

28

Innym sposobem jest sprawienie, aby android:layout_width="fill_parent" i android:layout_weight="1" to również działa dobrze !!!

+13

Naprawdę podekscytowałeś się tą odpowiedzią. :-) Uwielbiam ten entuzjazm! –

+0

Rozwiązania CommonsWare nie działały dla mnie, ale tak się stało! :) Użyłem "match_parent", ponieważ usłyszałem, że powinieneś preferować to nad "fill_parent", ale obie działają. – codepleb

1

Napisałem EqualSpaceLayout, który według mnie rozwiąże Twój problem. Jest to podobne do LinearLayout, ale nie zniekształca rozmiaru widoków podrzędnych. Sprawdź to: http://www.dev-smart.com/archives/225

16

Użyj LinearLayout z żądanym weightSum i utwórz elementy o takiej samej wartości: layout_weight. Oto przykład ...

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:weightSum="5"> 

    <ImageView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:src="@drawable/ic_share_white_36dp"/> 

    <ImageView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:src="@drawable/ic_search_white_36dp"/> 

    <ImageView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:src="@drawable/ic_event_note_white_36dp"/> 

    <ImageView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:src="@drawable/ic_brush_white_36dp"/> 

    <ImageView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:src="@drawable/ic_menu_white_36dp"/> 
</LinearLayout> 

Więc, suma masa wszystkich elementów wynosi 5. Oto zrzut ekranu ...

enter image description here

zauważyć, że Google Material Design ikony są używane. Mam nadzieję, że to jest pomocne.

Powiązane problemy