android – 了解measureWithLargestChild:为什么打破布局?

前端之家收集整理的这篇文章主要介绍了android – 了解measureWithLargestChild:为什么打破布局?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在玩选项measureWithLargestChild =“true”.我不明白为什么我的布局打破总计,如果我的一个按钮上有一个太大的文字.我觉得应该保持1/3的基本尺寸,但是它们会变小约1/6.这是为什么?

这里可以看到一些截图:

这是我的xml:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="fill_parent"
  3. android:layout_height="fill_parent"
  4. android:baselineAligned="false"
  5. android:gravity="center"
  6. android:orientation="vertical" >
  7. <LinearLayout
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:baselineAligned="false"
  11. android:measureWithLargestChild="true" >
  12. <Button
  13. style="@style/my_style"
  14. android:layout_weight="1"
  15. android:text="Button123456" />
  16. <Button
  17. style="@style/my_style"
  18. android:layout_weight="1"
  19. android:text="A" />
  20. <Button
  21. style="@style/my_style"
  22. android:layout_weight="1"
  23. android:text="WW" />
  24. </LinearLayout>
  25. </LinearLayout>

解决方法

我相信这是一个测量算法的错误. LinearLayout.measureHorizo​​ntal(int,int)方法中有以下注释.
  1. // Either expand children with weight to take up available space or
  2. // shrink them if they extend beyond our current bounds

它说,如果没有足够的空间,该算法将收缩物品的重量.当measureWithLargestChild设置为true时,所有项目的宽度与最左边的项目相同.现在他们都在一起不再适合父母的宽度了.因此他们会缩小.如果没有bug,所有的项目都会有相同的宽度.但是由于错误,算法收缩原始(wrap_content)宽度,而不是根据measureWithLargestChild属性计算的宽度.这就是为什么右边的两个项目变小了.

猜你在找的Android相关文章