android:layout_weight="1",What does android:layout_weight mean?

If there are multiple views spanning a LinearLayout, then layout_weight gives them each a proportional size. A view with a bigger layout_weight value "weighs" more, so it gets a bigger space.

Here is an image to make things more clear.

CyYBf.png

Theory

The term layout weight is related to the concept of weighted average in math. It is like in a college class where homework is worth 30%, attendance is worth 10%, the midterm is worth 20%, and the final is worth 40%. Your scores for those parts, when weighted together, give you your total grade.

cbfgn.png

It is the same for layout weight. The Views in a horizontal LinearLayout can each take up a certain percentage of the total width. (Or a percentage of the height for a vertical LinearLayout.)

The Layout

The LinearLayout that you use will look something like this:

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

Note that you must use layout_width="match_parent" for the LinearLayout. If you use wrap_content, then it won't work. Also note that layout_weight does not work for the views in RelativeLayouts (see here and here for SO answers dealing with this issue).

The Views

Each view in a horizontal LinearLayout looks something like this:

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1" />

Note that you need to use layout_width="0dp" together with layout_weight="1". Forgetting this causes many new users problems. (See this article for different results you can get by not setting the width to 0.) If your views are in a vertical LinearLayout then you would use layout_height="0dp", of course.

In the Button example above I set the weight to 1, but you can use any number. It is only the total that matters. You can see in the three rows of buttons in the first image that I posted, the numbers are all different, but since the proportions are the same, the weighted widths don't change in each row. Some people like to use decimal numbers that have a sum of 1 so that in a complex layout it is clear what the weight of each part is.

One final note. If you have lots of nested layouts that use layout_weight, it can be bad for performance.

Extra

Here is the xml layout for the top image:

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="android:layout_weight="

android:textSize="24sp" />

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="1" />

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="2"

android:text="2" />

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="1" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="android:layout_weight="

android:textSize="24sp" />

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="10"

android:text="10" />

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="20"

android:text="20" />

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="10"

android:text="10" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="android:layout_weight="

android:textSize="24sp" />

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight=".25"

android:text=".25" />

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight=".50"

android:text=".50" />

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight=".25"

android:text=".25" />

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值