了解android:layout_weight [英] Understanding android:layout_weight

查看:68
本文介绍了了解android:layout_weight的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么下面的清单仅显示第二个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会从屏幕上完全消失.

解决方案

来自

android:layout_height="match_parent" **,如上面的链接所述.

LinearLayout还支持为单个子项分配权重 使用android:layout_weight属性.此属性分配一个 一个视图的重要性"价值在于它应该有多少空间 占据屏幕上.较大的权重值使其可以扩展为 填充父视图中的所有剩余空间.子视图可以指定一个 权重值,则视图组中的任何剩余空间为 按他们声明的体重比例分配给孩子. 默认权重为零.

例如,如果有三个文本字段,并且其中两个声明一个 权重为1,而另一个不赋予权重,第三个文本字段 没有重量将不会增长,只会占据所需的面积 它的内容.另外两个将同等扩展以填充空间 在测量完所有三个字段之后剩下的.

如果第三个字段的权重为2(而不是0),则其权重为 现在被宣布为比其他两个都重要,因此它的一半 剩余的总空间,而前两个则平均分配其余的空间.

Why the following listing shows only the second TextView (red)?

<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>

I know that if I set android:layout_height="0px" it will only show the first TextView (green), and I understand this behavior.

But why when I set android:layout_height="match_parent", the first TextView disappear completely from the screen.

解决方案

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

It disappers because it second one acquires full space as it is given

android:layout_weight="0"

and

android:layout_height="match_parent"** as mentioned in above link.

LinearLayout also supports assigning a weight to individual children with the android:layout_weight attribute. This attribute assigns an "importance" value to a view in terms of how much space it should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view. Child views can specify a weight value, and then any remaining space in the view group is assigned to children in the proportion of their declared weight. Default weight is zero.

For example, if there are three text fields and two of them declare a weight of 1, while the other is given no weight, the third text field without weight will not grow and will only occupy the area required by its content. The other two will expand equally to fill the space remaining after all three fields are measured.

If the third field is then given a weight of 2 (instead of 0), then it is now declared more important than both the others, so it gets half the total remaining space, while the first two share the rest equally.

这篇关于了解android:layout_weight的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆