我正在玩选项measureWithLargestChild =“true”.我不明白为什么我的布局打破总计,如果我的一个按钮上有一个太大的文字.我觉得应该保持1/3的基本尺寸,但是它们会变小约1/6.这是为什么?
这里可以看到一些截图:
这是我的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>
解决方法
我相信这是一个测量算法的错误. LinearLayout.measureHorizontal(int,int)方法中有以下注释.
- // Either expand children with weight to take up available space or
- // shrink them if they extend beyond our current bounds
它说,如果没有足够的空间,该算法将收缩物品的重量.当measureWithLargestChild设置为true时,所有项目的宽度与最左边的项目相同.现在他们都在一起不再适合父母的宽度了.因此他们会缩小.如果没有bug,所有的项目都会有相同的宽度.但是由于错误,算法收缩原始(wrap_content)宽度,而不是根据measureWithLargestChild属性计算的宽度.这就是为什么右边的两个项目变小了.