了解measureWithLargestChild。为什么会破坏布局?

问题描述 投票:11回答:2

我在用选项 measureWithLargestChild="true". 我不明白为什么我的布局打破总,如果我的一个按钮有一个太大的文字到它。我认为它应该保持13的基本大小,但他们变得更小,约16。为什么会这样呢?

这里你可以看到一些截图。

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>
android android-layout android-linearlayout
2个回答
7
投票

我相信这是个测量算法的错误。有以下评论 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所有的项目都和最左边的项目有相同的宽度。现在,它们一起不再适合父项的宽度。因此它们会被缩小。如果没有这个错误,所有的项目都会有相同的宽度。但是由于这个bug,算法缩小了。原有 (wrap_content)的宽度,而不是根据以下方法计算的宽度 measureWithLargestChild 属性。这就是为什么右边的两个项目变小了。


0
投票

例如,如果你有一个水平的LinearLayout,并带有 android:measureWithLargestChild="true"android:layout_width="wrap_content" 确保子视图已经设置了两个 android:layout_width="0dp"android:layout_weight="1". 否则孩子的宽度不是你所期望的。

© www.soinside.com 2019 - 2024. All rights reserved.