Android RelativeLayout and childs with layout_width = "match_parent"

How does Android calculate the size of a view when I have two views in the same "row", one with a width = "fill_parent"?

Example:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:layout_width="fill_parent"> <EditText android:id="@+id/EditText01" android:hint="Enter some text..." android:layout_alignParentLeft="true" android:layout_width="match_parent" android:layout_toLeftOf="@+id/Button01" android:layout_height="wrap_content"></EditText> <Button android:id="@+id/Button01" android:text="Press Here!" android:layout_width="wrap_content" android:layout_alignParentRight="true" android:layout_height="wrap_content"></Button> </RelativeLayout> 

This code gives all the EditText free space and shows the button to the right of it. But with these changes, the EditText fills the entire width, and the button does not appear on the screen:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:layout_width="fill_parent"> <EditText android:id="@+id/EditText01" android:hint="Enter some text..." android:layout_alignParentLeft="true" android:layout_width="match_parent" android:layout_height="wrap_content"></EditText> <Button android:id="@+id/Button01" android:text="Press Here!" android:layout_width="wrap_content" android:layout_toRightOf="@+id/EditText01" android:layout_height="wrap_content"></Button> </RelativeLayout 
+4
source share
3 answers

android:layout_toLeftOf="@+id/Button01" will make the right edge of the EditText align to the left side of the Button , so Android ignores the match_parent width, forcing the width to fill only the left parent side to the right of the button side.

android:layout_toRightOf="@+id/EditText01" will cause the left border of the Button align with the right side of the EditText , but since the width of the EditText is match_parent , the right side is aligned with the right side of the parent view and Android will just make the button leave the screen.

+7
source

In both examples, <EditText/> matches the entire width of the screen. In the first example, Button is independent of <EditText/> , so it can be aligned to the right edge of the screen. In the second example, however, the Button should align to the right of `, so it pops out of view.

+1
source

If you use RelativeLayout , then you have no β€œlines”, all you have is a space in which you can arrange your views. If you want to save the button on the right side of the screen and fill the EditText with the rest of the screen, you can do something like this:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:layout_width="fill_parent"> <EditText android:id="@+id/EditText01" android:hint="Enter some text..." android:layout_toLeftOf="@+id/Button01" android:layout_width="match_parent" android:layout_height="wrap_content"></EditText> <Button android:id="@+id/Button01" android:text="Press Here!" android:alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </RelativeLayout/> 
0
source

Source: https://habr.com/ru/post/1480733/


All Articles