enter image description here

enter image description here

At the top are two pictures, in one of them a Button (QWERTY2) loses its true form when it contains the number of lines unequal to the number its neighbor has on the left (QWERTY1) (the neighbor has 1 line, and that 2). The same situation can occur with Button QWERTY1, relative to QWERTY2. Naturally, when they have an equal number of lines, the buttons do not lose their shape, as you noticed from the first picture. In contrast, with the QWERTY3, QWERTY4 buttons, this does not happen. How to fix this problem so that the buttons retain their normal form, regardless of the number of words or lines in them? Below is my XML code:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000000"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="count" android:layout_toLeftOf="@+id/amount_of_true_answers" android:layout_toRightOf="@+id/amount_of_false_answers" android:textSize="22dp" android:gravity="center" android:id="@+id/questCounterId" android:textColor="#FFFFFF" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="true" android:layout_marginRight="7dp" android:id="@+id/amount_of_true_answers" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:textSize="22dp" android:textColor="#007E41" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Question" android:textSize="24dp" android:id="@+id/QuestionText" android:layout_above="@+id/nextButton" android:layout_below="@+id/questCounterId" android:gravity="center" android:textColor="#FFFFFF" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="false" android:id="@+id/amount_of_false_answers" android:textSize="22dp" android:layout_marginLeft="7dp" android:layout_alignParentTop="true" android:textColor="#861F23" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/a_and_b" android:id="@+id/nextButton" android:onClick="ShowNextQuestion" android:layout_marginBottom="10dp" android:text=">" android:layout_centerHorizontal="true" android:background="@drawable/using_nextprevbuttons" android:shadowColor="#FFFFFF" android:shadowDx="0" android:shadowDy="0" android:shadowRadius="4" android:textSize="32dp" /> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@+id/c_and_d" android:id="@+id/a_and_b" android:layout_marginBottom="10dp" > <Button android:onClick="OptionButtonPressed" android:id="@+id/opta" android:text="qwerty1" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:background="@drawable/using_optionshape" android:textColor="#000000" android:shadowColor="#FFFFFF" android:shadowDx="0" android:shadowDy="0" android:shadowRadius="5" android:layout_marginRight="10dp" android:layout_marginLeft="5dp" /> <Button android:id="@+id/optb" android:onClick="OptionButtonPressed" android:text="qwerty2 qwerty2 qwerty2 " android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:background="@drawable/using_optionshape" android:textColor="#000000" android:shadowColor="#FFFFFF" android:shadowDx="0" android:shadowDy="0" android:shadowRadius="5" android:layout_marginRight="5dp" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/c_and_d" android:layout_alignParentBottom="true" android:layout_marginBottom="10dp" > <Button android:text="qwerty3" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:background="@drawable/using_optionshape" android:textColor="#000000" android:shadowColor="#FFFFFF" android:shadowDx="0" android:shadowDy="0" android:shadowRadius="5" android:layout_marginRight="10dp" android:layout_marginLeft="5dp" android:onClick="OptionButtonPressed" android:id="@+id/optc" /> <Button android:text="qwerty4" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:background="@drawable/using_optionshape" android:textColor="#000000" android:shadowColor="#FFFFFF" android:shadowDx="0" android:shadowDy="0" android:shadowRadius="5" android:layout_marginRight="5dp" android:id="@+id/optd" android:onClick="OptionButtonPressed" /> </LinearLayout> </RelativeLayout> 

    1 answer 1

    You can set your LinearLayout (whose id a_and_b) parameter android:gravity="center" , then everything that is inside it will be centered relative to itself

    Otherwise, you can set your button (qwerty2 which) is the parameter android:layout_gravity="center" , then it alone will be centered relative to the given LinearLayout.

    But, even when everything is centered, the size of the button will still be slightly more than others, because its height is wrap_content, i.e. height is determined by the content of the component. The more text you write into it, the more it will stretch.

    You can make the size of the buttons fixed, but better, play around with LinearLayout, make LinearLayout root and split the page into three internal layouts, the upper RelativeLayout and the two lower LinearLayout: in the RelativeLayour place your texts and the button with the ">", and in the lower two place your buttons (as you did).

    Next, set the weight of these layouts, suppose the top layout is 70, and the bottom two with buttons will be 15 each, and then set the height of the match_parent. The buttons will stretch across the entire height of the layout, you get what you need. Set the root layout parameter android: weightSum = "100"

    Here is an example:

     <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:weightSum="100"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="80"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_weight="10"> <Button android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="qwerty1"/> <Button android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="qwerty2 qwerty2 qwerty2"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_weight="10"> <Button android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="qwerty3" android:id="@+id/button2"/> <Button android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="qwerty4"/> </LinearLayout> </LinearLayout> 

    enter image description here