## LinearLayout的weight属性的解释 顶原

林泳坛

In a nutshell, `layout_weight` specifies how much of the extra space in the layout to be allocated to the View.

LinearLayout supports assigning a weight to individual children. This attribute assigns an "importance" value to a view, and allows it to expand to fill any remaining space in the parent view. Views' default weight is zero.

# Calculation to assign any remaining space between child

In general, the formula is:

space assigned to child = (child's individual weight) / (sum of weight of every child in Linear Layout)

## Example 1

If there are three text boxes and two of them declare a weight of 1, while the third one is given no weight (0), then remaining space is assigned as follows:

1st text box = 1/(1+1+0)

2nd text box = 1/(1+1+0)

3rd text box = 0/(1+1+0)

## Example 2

Let's say we have a text label and two text edit elements in a horizontal row. The label has no `layout_weight` specified, so it takes up the minimum space required to render. If the `layout_weight`of each of the two text edit elements is set to 1, the remaining width in the parent layout will be split equally between them (because we claim they are equally important).

Calculation:

1st label = 0/(0+1+1)

2nd text box = 1/(0+1+1)

3rd text box = 1/(0+1+1)

If, instead, the first one text box has a `layout_weight` of 1, and the second text box has a `layout_weight` of 2, then one third of the remaining space will be given to the first, and two thirds to the second (because we claim the second one is more important).

Calculation:

1st label = 0/(0+1+2)

2nd text box = 1/(0+1+2)

3rd text box = 2/(0+1+2)

Source article

Copied by 啪嗒科技

© 著作权归作者所有

### 林泳坛

Android:layout_weight详解 中文定义： 在一个布局组件中（例如：LinearLayout）中各个子元素的对于剩余空间的使用权重。 默认：android:layout_weight= 0 默认情况下，布局模式以android:h...

2012/03/05
532
0
2.2.1 LinearLayout(线性布局)

2.2.1 LinearLayout(线性布局) 分类 Android 基础入门教程 本节引言 本节开始讲Android中的布局，Android中有六大布局,分别是: LinearLayout(线性布局)，RelativeLayout(相对布局)，TableLa...

2016/03/21
31
0
android：layout_weight属性详解

2013/06/06
0
0
android：layout_weight属性详解

在android开发中LinearLayout很常用，LinearLayout的内控件的android:layoutweight在某些场景显得非常重要，比如我们需要按比例显示。android并没用提供table这样的控件，虽然有TableLayo...

2014/03/19
26
0
android:layout_weight属性详解

weight：重量、权重。 　　当我们给一个view设置了android:layout_weight属性，意味着赋予它话语权，常规思维就是谁的weight大，谁说了算（空间占比大）。 　　下面我们来看下具体的代码： ...

04/02
0
0

shangcheng-my

1.数据库主键、外键类型为bigint，那么在后台应该用什么类型的变量定义？ 后台用string接收，因为前段传过来的一般都是json字符串，后台直接接收，mysql是可以吧数字类型的字符串转换为对应的...

2
0

5
0
Centos7 安装zabbix-agent

rpm -i https://repo.zabbix.com/zabbix/4.2/rhel/6/x86_64/zabbix-release-4.2-2.el6.noarch.rpm 可以到https://repo.zabbix.com/zabbix找到对应的版本 yum install zabbix-agent -y 出现E......

abowu

8
0

GNU nano 4.4 "Hagelslag" 更新日志： 启动时，光标可以放在第一个或最后一个出现位置 字符串前面带有+/string 或 +?string的字符串。 发生自动硬包装时（(--breaklonglines），任何前导引号...

linuxCool

7
0

5
0