I want to know: What is android:weightSum and layout weight, and how do they work?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
From developer documentation
This can be used for instance to give a single child
50%
of the total available space by giving it a layout_weight of0.5
and setting the weightSum to1.0
.Addition to @Shubhayu answer
rest
3/5
can be used for other child layouts which really doesn't need any specific portion of containing layout.this is potential use of
android:weightSum
property.After some experimenting, I think the algorithm for LinearLayout is this:
Assume that
weightSum
is set to a value. The case of absence is discussed later.First, divide the
weightSum
by the number of elements whithmatch_parent
orfill_parent
in the dimension of the LinearLayout (e.g.layout_width
fororientation="horizontal"
). We will call this value the weight multiplier for each element. The default value forweightSum
is 1.0, so the default weight multiplier is1/n
, wheren
is the number offill_parent
elements;wrap_content
elements do not contribute ton
.E.g. when
weightSum
is 60, and there are 3fill_parent
elements, the weight multiplier is 20. The weight multiplier is the default value for e.g.layout_width
if the attribute is absent.Second, the maximum possible expansion of every element is computed. First, the
wrap_content
elements are computed according to their contents. Their expansion is deducted from the expansion of the parent container. We will call the remainerexpansion_remainer
. This remainder is distributed amongfill_parent
elements according to theirlayout_weight
.Third, the expansion of every
fill_parent
element is computed as:Example:
If
weightSum
is 60, and there are 3fill_parent
elements with the weigths 10, 20 and 30, their expansion on the screen is 2/3, 1/3 and 0/3 of the parent container.The minimum expansion is capped at 0. The maximum expansion is capped at parent size, i.e. weights are capped at 0.
If an element is set to
wrap_content
, its expansion is calculated first, and the remaining expansion is subject to distribution among thefill_parent
elements. IfweightSum
is set, this leads tolayout_weight
having no effect onwrap_content
elements. However,wrap_content
elements can still be pushed out of the visible area by elements whose weight is lower than (e.g. between 0-1 forweightSum
= 1 or between 0-20 for the above example).If no
weightSum
is specified, it is computed as the sum of alllayout_weight
values, including elements withwrap_content
set! So havinglayout_weight
set onwrap_content
elements, can influence their expansion. E.g. a negative weight will shrink the otherfill_parent
elements. Before thefill_parent
elements are laid out, will the above formula be applied towrap_content
elements, with maximum possible expansion being their expansion according to the wrapped content. Thewrap_content
elements will be shrunk, and afterwards the maximum possible expansion for the remainingfill_parent
elements is computed and distributed.This can lead to unintuitive results.
Weight sum works exactly as you want (like other answers you don't have to sum all the weights on parent layout). On child view specify the weight you want it to take. Don't forget to specify
Following is an example
This will look like