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
The documentation says it best and includes an example, (highlighting mine).
So to correct superM's example, suppose you have a
LinearLayout
with horizontal orientation that contains twoImageViews
and aTextView
with. You define theTextView
to have a fixed size, and you'd like the twoImageViews
to take up the remaining space equally.To accomplish this, you would apply
layout_weight
1 to eachImageView
, none on theTextView
, and aweightSum
of 2.0 on theLinearLayout
.Per documentation,
android:weightSum
defines the maximum weight sum, and is calculated as the sum of thelayout_weight
of all the children if not specified explicitly.Let's consider an example with a
LinearLayout
with horizontal orientation and 3ImageViews
inside it. Now we want theseImageViews
always to take equal space. To acheive this, you can set thelayout_weight
of eachImageView
to 1 and theweightSum
will be calculated to be equal to 3 as shown in the comment.weightSum
is useful for having the layout rendered correctly for any device, which will not happen if you set width and height directly.One thing which seems like no one else mentioned: let's say you have a vertical
LinearLayout
, so in order for the weights in layout/element/view inside it to work 100% properly - all of them must havelayout_height
property (which must exist in your xml file) set to0dp
. Seems like any other value would mess things up in some cases.Layout Weight works like a ratio. For example, if there is a vertical layout and there are two items(such as buttons or textviews), one having layout weight 2 and the other having layout weight 3 respectively. Then the 1st item will occupy 2 out of 5 portion of the screen/layout and the other one 3 out of 5 portion. Here 5 is the weight sum. i.e. Weight sum divides the whole layout into defined portions. And Layout Weight defines how much portion does the particular item occupies out of the total Weight Sum pre-defined. Weight sum can be manually declared as well. Buttons, textviews, edittexts etc all are organized using weightsum and layout weight when using linear layouts for UI design.
Adding on to superM's and Jeff's answer,
If there are 2 views in the LinearLayout, the first with a layout_weight of 1, the second with a layout_weight of 2 and no weightSum is specified, by default, the weightSum is calculated to be 3 (sum of the weights of the children) and the first view takes 1/3 of the space while the second takes 2/3.
However, if we were to specify the weightSum as 5, the first would take 1/5th of the space while the second would take 2/5th. So a total of 3/5th of the space would be occupied by the layout keeping the rest empty.
If unspecified, the sum is computed by adding the layout_weight of all of the children. This can be used for instance to give a single child 50% of the total available space by giving it a layout_weight of 0.5 and setting the weightSum to 1.0. Must be a floating point value, such as "1.2"