gpt4 book ai didi

android - 了解 measureWithLargestChild : Why breaks the layout?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:09:27 26 4
gpt4 key购买 nike

我正在使用选项 measureWithLargestChild="true"。我不明白为什么我的布局完全中断,如果我的一个按钮上有太大的文本。我认为它应该保持 1/3 的基本尺寸,但它们会变小约 1/6。这是为什么?

这里可以看到一些截图:

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>

最佳答案

我认为这是测量算法中的错误。 LinearLayout.measureHorizo​​ntal(int, int) 方法中有如下注释。

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

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

关于android - 了解 measureWithLargestChild : Why breaks the layout?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16356333/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com