2013-05-03 23 views
10

Gram z opcją measureWithLargestChild="true". Nie rozumiem, dlaczego mój układ łamie się całkowicie, jeśli jeden z moich przycisków ma na nim zbyt duży tekst. Myślę, że powinien zachować podstawowy rozmiar 1/3, ale zmniejsza się o 1/6. Dlaczego?Opis pomiaruWithLargestChild: Dlaczego łamie układ?

Tutaj można zobaczyć kilka zrzutów ekranu:

Button layout bug

Oto mój xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:baselineAligned="false" 
    android:gravity="center" 
    android:orientation="vertical" > 
    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:baselineAligned="false" 
     android:measureWithLargestChild="true" > 
     <Button 
      style="@style/my_style" 
      android:layout_weight="1" 
      android:text="Button123456" /> 
     <Button 
      style="@style/my_style" 
      android:layout_weight="1" 
      android:text="A" /> 
     <Button 
      style="@style/my_style" 
      android:layout_weight="1" 
      android:text="WW" /> 
    </LinearLayout> 
</LinearLayout> 

Odpowiedz

6

Wierzę, że jest to błąd w algorytmie pomiarowym. Istnieje następujący komentarz w metodzie LinearLayout.measureHorizontal(int, int).

// Either expand children with weight to take up available space or 
// shrink them if they extend beyond our current bounds 

Mówi, algorytm skurczy przedmiotów z weight jeśli nie ma wystarczająco dużo miejsca dla nich. Ponieważ measureWithLargestChild ma wartość true, wszystkie elementy mają tę samą szerokość, co najbardziej pozostawiony element. Teraz wszyscy razem nie pasują już do szerokości rodziców. W ten sposób będą się skurczyć. Gdyby nie było błędu, wszystkie elementy miałyby później taką samą szerokość. Ale z powodu błędu algorytm kurczy się o pierwotnych szerokości (wrap_content) zamiast szerokości obliczanych według atrybutu measureWithLargestChild. Dlatego dwa przedmioty po prawej stały się mniejsze.

+0

Czy ktoś może wyjaśnić, dlaczego jest skurczony w przypadku 2 (na 4 przypadki). Dlaczego nie ma w tym przypadku miejsca? – Diffy