理解android:layout_weight

3
为什么以下代码列表只显示第二个TextView(红色)?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="11111"
        android:background="#00FF00" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0"
        android:background="#FF0000"
        android:text="00000"/>
</LinearLayout>

我知道如果我设置android:layout_height="0px",它只会显示第一个TextView(绿色),我理解这种行为。但是为什么当我设置android:layout_height="match_parent"时,第一个TextView完全从屏幕上消失了呢?

1
所有有权重的视图的权重尺寸 必须0dp。而将一个视图设为权重为 1,另一个视图设为权重为 0 不是一个好主意。使用 android:visibility="GONE" 隐藏一个视图。 - Phantômaxx
尝试查看此帖子 https://dev59.com/xG865IYBdhLWcg3wFqdV - StefanoM5
7个回答

4

来自https://developer.android.com/guide/topics/ui/layout/linear.html

它消失了,因为第二个视图占满了屏幕,由于它的属性是:

android:layout_weight="0"

android:layout_height="match_parent",如上链接所述。

LinearLayout还支持使用android:layout_weight属性为单个子视图分配重量。该属性根据视图在屏幕上占用空间的重要性值进行分配。较大的权重值允许它扩展以填充父视图中的任何剩余空间。子视图可以指定权重值,然后视图组中的任何剩余空间都按其声明的权重比例分配给子视图。默认权重为零

例如,如果有三个文本字段,其中两个声明了权重为1,而另一个没有权重,则没有权重的第三个文本字段不会增长,并且只占用其内容所需的区域。其他两个将平均扩展以填充测量所有三个字段后剩余的空间。

如果第三个字段然后被赋予权重2(而不是0),则它现在被声明比其他两个更重要,因此它获得总剩余空间的一半,而前两个平均分享其余的空间。


1

android:layout_weight="1" 的意思是你将未被其他视图占用的剩余空间分配给该视图...所以在你的情况下,second TextView 是match_parent,所以没有空白留下,这就是为什么first TextView 不可见的原因。

P.S:将weightsum传递给父布局,并根据需要在子视图之间分配该权重。
谢谢


0

你已将第一个按钮的权重值设置为1,第二个按钮的权重值设置为0,因此第一个按钮将覆盖整个空间

你应该给父布局一个总权重,并根据所需比例在子元素之间分配总权重。


0

0

你的代码基本上是在说,给我的第一个文本视图所有的空间,而我的第二个文本视图没有,尝试给每个子元素分配一个权重为1,这应该会给它们相等的间距。另外,由于你有一个垂直方向的父元素,在子元素中设置布局高度与父元素匹配与权重属性相矛盾。因此,请将其删除并替换为wrap content。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="11111"
    android:background="#00FF00" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="#FF0000"
    android:text="00000"/>
  </LinearLayout>

包装内容将围绕您的文本进行包装,为文本大小添加属性会非常有用。 - JohnChris

0

android:layout_weight表示你希望这些视图相对于彼此和父视图的显示方式。 例如,如果你有以下代码:

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button2"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button1"/>
    </LinearLayout>

结果将是这张图片

如果我将Button1的android:layout_weight从1改为2,而Button2的权重仍为1,则Button1的宽度将比Button2大两倍


0

但是为什么,如果我在第二个TextView中设置:

android: layout_weight = "0.1" 

如果我设置:

TextView的第二个组件将占用较少的空间。

android: layout_weight = "0"

如果我在第二个TextView中设置

android: layout_weight = "0.2"

TextView的第二个组件将占用比设置android:layout_weight="0.1"更少的空间。

因此,如果我设置android:layout_weight的值更高,它将占用更小的空间。


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