Worth the next challenge. There are 5 buttons with a fixed width each (may vary). It is necessary to place them in one line with the same gap between them (depending on the screen resolution). Is it possible to somehow impose such in xml, since I do not want to programmatically.

PS: The width of the buttons should not change. It is necessary to change the gap between them.

  • one
    Insert between the view buttons, which will serve as spacers, and for them already assign layout_weight. Of course, all this should be inside LinearLayout with orientation = "horizontal". Or, the second option, to wrap each button in RelativeLayout, for buttons set layout_centerHorizontal = "true", and for RelativeLayout - layout_weight = "0.2". All this is thrust, again, in LinearLayout. - falstaf
  • I thought about this, but somehow this is a crutch. - st_er
  • I'm afraid, otherwise nothing, if it is XML-layout'om solve the problem. - falstaf

3 answers 3

Yes, you can do this using the LinearLayout property - weightSum

You need to create LinearLayout c weightSum = 5 , then for each child specify layout_weight = 1 .

Thus, the width of the LinearLayout is divided equally into 5 parts and each child will have a width equal to one fifth of the parent width.

  • one
    Or set the buttons for layout_weight = "0.2" and then do not need to change the weightSum. - falstaf
  • yes it is possible and so by the way - Roman Zakharov
  • The fact of the matter is that the width of the buttons should not change, the gaps between them should change - st_er

You need to place them in LinearLayout with a horizontal orientation and for each button, set margins for each button. It is written in detail here .

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <Button android:id="@+id/button1" android:layout_width="5dp" android:layout_height="wrap_content" android:layout_weight="1" /> <Button android:id="@+id/button2" android:layout_width="5dp" android:layout_height="wrap_content" android:layout_weight="1" /> <Button android:id="@+id/button3" android:layout_width="5dp" android:layout_height="wrap_content" android:layout_weight="1" /> <Button android:id="@+id/button4" android:layout_width="5dp" android:layout_height="wrap_content" android:layout_weight="1" /> <Button android:id="@+id/button5" android:layout_width="5dp" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout> 
    • Well, in the first place, don't you need weightSum? and secondly, isn’t it appropriate in this case to make layout_width equal to 0dp? - rasmisha
    • @rasmisha without weightSum works fine, layout_width = 0dp, the buttons disappear when the editor wants it, it shows warning! - katso
    • @katso seriously something great? Even it became interesting to check, in the evening I will look at home even on purpose. And I didn’t remember the warning messages - rasmisha