Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Layout buttons so each divides up the space equally

Im using a LinearLayout to put two buttons horizontally side-by-side, but I want to each button to size itself to use 50% of the horizontal space. I thought layout_weight of "1" for each button would do the trick, but maybe my layout_width needs to be changed?

like image 925
Eno Avatar asked Feb 17 '10 23:02

Eno


People also ask

What does layout weight do?

Layout Weight This attribute assigns an "importance" value to a view in terms of how much space it should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view.

Is it possible to evenly distribute buttons across the width of an android Linearlayout?

Expanding on fedj's answer, if you set layout_width to 0dp and set the layout_weight for each of the buttons to 1, the available width will be shared equally between the buttons.


1 Answers

The layout_weight attribute controls how much of the left over space each of your buttons is given. If your buttons take up different amounts of space to start with, then each of them will first be given the space they ask for, and then any remaining space will be divided up between them, meaning that you won't have an exact 50/50 split.

You can get around this behaviour by first setting layout_width="0px" (keeping your layout_weights as they are), and relying on 50% being enough space to display each button.

like image 84
Kai Avatar answered Oct 01 '22 02:10

Kai