DJ180
DJ180

Reputation: 19854

Views taking equal share of Layout

I've created a custom dialog builder that contains 2 buttons.

Depending on the dialog's setup, I may choose to hide one of the buttons completely, using Window.GONE.

Ideally what I want to happen is: 1. If there is only one button, then fill the layout with it 2. If there are two buttons, then split up the space in the layout equally with these

Is it possible to do this without having to work out the width of the dialog, the number of buttons and then set the sizes manually? I was hoping there may be a neater way to perform this

Upvotes: 1

Views: 109

Answers (2)

user1217690
user1217690

Reputation:

yes of cource put your views means button in linear layout and give yor buttons equal layout_weight will solve your problem.

Upvotes: 1

edthethird
edthethird

Reputation: 6263

ok, here is how I would do it:

<LinearLayout layout_width:fill_parent layout_height:wrap_content>

    <Button
       layout_width=fill_parent
       layout_height=wrap_content
       layout_weight=1/>
    <Button
       layout_width=fill_parent
       layout_height=wrap_content
       layout_weight=1/>
</LinearLayout>

The trick is to put both elements a width of fill_parent and a weight of 1. If they are both drawn, they will each take up half the screen. If you use View.Gone, one of them will disappear and the other should take up all the space.

Upvotes: 2

Related Questions