Android layout weight explained

Layout weight is used to determine how Android divides up any left over space after the various widgets get all of the space they want.

Layout weight example

So say you have 3 widgets in a row, each of which want 20 pixels, but you have 100 pixels worth of space. Here are a few examples of layout_weights and the number of pixels each widget will claim:

widget1: weight = 1, 40px
widget2: weight = 1, 40px
widget3: weight = 0, 20px

widget1: weight = 1, 33px
widget2: weight = 1, 33px
widget3: weight = 1, 33px

You can think of the weight as a percentage of available space. It will add up all of the values, and then give out portions as appropriate. Thus, if it helps you can use weights that add up to 100 to do percentages:

widget1: weight = 0, 20px
widget2: weight = 40, 32px
widget3: weight = 60, 48px

Leave a comment

Your email address will not be published. Required fields are marked *