I have three buttons

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:layout_height="wrap_content" android:layout_width="match_parent" android:text="Красный" android:gravity="center" android:layout_alignParentTop="true"/> <Button android:layout_height="wrap_content" android:layout_width="match_parent" android:text="Зелёный" android:gravity="center"/> <Button android:layout_height="wrap_content" android:layout_width="match_parent" android:text="Синий" android:gravity="center" android:layout_alignParentBottom="true"/> </LinearLayout> 

How to make, what the first button would be from above, the second - in the center, and the third from below?

    2 answers 2

    You can add views between the buttons, which will take all the remaining space on the screen, by dividing it equally with the help of weight:

     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:layout_height="wrap_content" android:layout_width="match_parent" android:text="Красный" android:gravity="center" android:layout_alignParentTop="true"/> <Space android:layout_height="0dp" android:layout_width="match_parent" android:layout_weight="1"/> <Button android:layout_height="wrap_content" android:layout_width="match_parent" android:text="Зелёный" android:gravity="center"/> <Space android:layout_height="0dp" android:layout_width="match_parent" android:layout_weight="1"/> <Button android:layout_height="wrap_content" android:layout_width="match_parent" android:text="Синий" android:gravity="center" android:layout_alignParentBottom="true"/> </LinearLayout> 

    Or replace their parents with RelativeLayout and set the property for the middle button

     android:centerInParent="true" 
       <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:text="Button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button1" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> <Button android:text="Button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button2" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> <Button android:text="Button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button3" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" /> </RelativeLayout> 
      • And how to make sure that when you press the button, the background color changes? - maksimumka
      • Oooh, yes this is a whole business)) look for startandroid lessons - Dmitry Vedmed
      • I'm just new to this business - maksimumka
      • And I can not google - maksimumka
      • A link can you throw? - maksimumka