I don't understand how to use this attribute. Can anyone tell me more about it?
相关问题
- 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
For additional:
For
vertical
orientation, don't forget setheight
to 0dpFor
horizontal
orientation, don't forget setwidth
to 0dphttp://developer.android.com/guide/topics/ui/layout-objects.html#linearlayout
layout_weight defines how much space the control must obtain respectively to other controls.
As the name suggests, Layout weight specifies what amount or percentage of space a particular field or widget should occupy the screen space.
If we specify weight in horizontal orientation, then we must specify
layout_width = 0px
.Similarly, If we specify weight in vertical orientation, then we must specify
layout_height = 0px
.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:
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:
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 thelayout_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:
If, instead, the first one text box has a
layout_weight
of 1, and the second text box has alayout_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:
Source article
adding to the other answers, the most important thing to get this to work is to set the layout width (or height) to 0px
otherwise you will see garbage
layout_weight
tells Android how to distribute yourView
s in aLinearLayout
. Android then first calculates the total proportion required for allView
s that have a weight specified and places eachView
according to what fraction of the screen it has specified it needs. In the following example, Android sees that theTextView
s have alayout_weight
of0
(this is the default) and theEditText
s have alayout_weight
of2
each, while theButton
has a weight of1
. So Android allocates 'just enough' space to displaytvUsername
andtvPassword
and then divides the remainder of the screen width into 5 equal parts, two of which are allocated toetUsername
, two toetPassword
and the last part tobLogin
:It looks like:
and