Center two buttons horizontally

后端 未结 18 1277
我在风中等你
我在风中等你 2020-12-12 13:32

I try to arrange two buttons (with images on them which work fine) next to each other and to center them horizontally. That\'s what I have so far:



        
18条回答
  •  执念已碎
    2020-12-12 14:29

    The most elegant way to go about this without introducing redundant components or view groups is to use spacing. You can use space elements with layout_weight within a linear layout to get the effect you want:

    
            
            

    Giving both space elements an equal layout_weight (doesn’t matter what the weights are, it just takes them as a ratio out of the totals of all weights) causes the space elements to take up the extra space that’s available. So it forces the buttons to the middle, which don’t have any layout_weight assigned to them, so they don’t take any extra space, just what they are explicitly given (the size of the images).

提交回复
热议问题