2013-05-03 106 views
10

我正在玩選項measureWithLargestChild="true"。如果我的某個按鈕的文字太大,我不明白爲什麼我的佈局會完全打破。我認爲它應該保持1/3的基本尺寸,但是它們會變小約1/6。這是爲什麼?瞭解measureWithLargestChild:爲什麼打破布局?

在這裏你可以看到一些截圖:

Button layout bug

這裏是我的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> 

回答

6

我相信這是測量算法的錯誤。在LinearLayout.measureHorizontal(int, int)方法有以下評論。

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

它說,該算法將縮小與weight項目如果沒有他們足夠的空間。由於measureWithLargestChild設置爲true,因此所有項目都與最左邊的項目具有相同的寬度。現在他們都在一起不再適合父母的寬度了。因此它們會縮小。如果沒有錯誤,則所有項目之後都會有相同的寬度。但由於該錯誤,算法會縮小原始的(wrap_content)寬度,而不是根據measureWithLargestChild屬性計算的寬度。這就是爲什麼右側的兩個項目變小了。

+0

有人能解釋爲什麼它收縮在情況2(4例中)。這種情況下怎麼沒有空間? – Diffy 2014-07-25 11:47:53