Android布局权重行为

4
我正在尝试使用“layout_weight”来构建适合所有设备大小的Android布局,并且我在理解其行为时遇到了一些困难。我注意到更改“layout_width” / “layout_height”会影响“layout_weight”的行为,但是我不明白其原理。例如,我想要一个垂直的“LinearLayout”,将其分成三个内部“LinearLayout”,使得顶部和底部的填充屏幕的比例为25%,中间的比例为50%,而不受内部布局内容的影响(如果内部“LinearLayout”的内容太大,则不应移动其他布局)。为了实现这一点,我应该将内部“LinearLayout”的“layout_height”属性设置为“fill_parent”还是“wrap_content”?谢谢!编辑:看起来“layout_weight”与布局所占大小成反比例关系。以下是三个示例: 1/1/1权重(按照我的预期工作)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
        <LinearLayout
            android:id="@+id/layout1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="#FF0000"/> //RED
        <LinearLayout
            android:id="@+id/layout2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="#00FF00"/> //GREEN
        <LinearLayout
            android:id="@+id/layout3"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="#0000FF"/> //BLUE
</LinearLayout>

结果: 在此输入图片描述

权重 1/2/1 (为什么是这个权重?)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
        <LinearLayout
            android:id="@+id/layout1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="#FF0000"/> //RED
        <LinearLayout
            android:id="@+id/layout2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="2"
            android:background="#00FF00"/> //GREEN
        <LinearLayout
            android:id="@+id/layout3"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="#0000FF"/> //BLUE
</LinearLayout>

结果: 在此输入图片描述

**权重3/2/3(我原打算使用1/2/1):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
        <LinearLayout
            android:id="@+id/layout1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="3"
            android:background="#FF0000"/> //RED
        <LinearLayout
            android:id="@+id/layout2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="2"
            android:background="#00FF00"/> //GREEN
        <LinearLayout
            android:id="@+id/layout3"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="3"
            android:background="#0000FF"/> //BLUE
</LinearLayout>

结果:输入图像描述
3个回答

3
为了使 layout_weight 能够按照预期(和文档)工作,您必须不要layout_height 设置为 fill_parentmatch_parent
右侧的示例 http://developer.android.com/guide/topics/ui/layout/linear.html#Weight 显示了一个将 layout_height 设置为 0dp 的示例。但是,不设置它或将其设置为 wrap_content 也可以使 layout_weight 按预期工作。

3
您可以尝试将layout_height = 0dp设置为零。

1
你应该将三个内部布局的layout_height属性设置为"fill_parent",然后更改它们的权重以使它们看起来符合你的要求。

@Egor:我尝试过这样做,但我不明白layout_weight在这种情况下的作用:如果我放置1/1/1,那么三个部分将是相等的。但如果我放置1/2/1,我看不到第二个部分,屏幕只是被第一个布局和第三个布局平均分成了50%。它是如何工作的? - nbarraille
@nbarraille: 你应该发布有问题的布局,这样我们可以更好地评估特定情况下出现了什么问题,因为空间应该按照你指定的分配。 - dmon
2
@Enbarraille:你也可以使用2:1:2的权重,而不是3:2:3。我也觉得权重成反比例很奇怪,似乎与直觉相反。 - Spidy
1
@nbarraille:不错的示例!是的,我认为你搞错了,“数字越小,重要性越高”。话虽如此,我仍然不知道为什么1/2/1会完全去掉中间的容器。我想这可能与它没有内容有关。你试过用三个带文本的按钮吗?看看中间的那个是否仍然消失了? - dmon
@dmon:在布局中放置一个按钮并没有帮助,第二个按钮在1/2/1中仍然没有显示出来。 - nbarraille
@Spidy:我同意,如果只是使用权重分配(weight/sum_of_weights)*100%的布局给每个元素,那将会简单得多...我认为它们甚至不是反比例关系(否则2/1/2和3/2/3就不会相同)。你有没有想出一些将权重转换为百分比的公式? - nbarraille

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接