Align two buttons horizontally

I have two buttons, and I want to place them horizontally, so that they will be next to each other on the screen, there is no space left, I know that I can do this with a linear pause and set the weight, but I want relativelayout , because I already have weight in my XML, so there will be poor performance to put two weights into each other.

I tried like this

<RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/bg_selector" > <Button android:id="@+id/b_orderMeal_send" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_marginLeft="4dip" android:layout_marginRight="4dip" android:background="@drawable/button_selector" android:clickable="true" android:focusable="true" android:text="@string/b_send" android:textColor="#FFFFFF" /> <Button android:id="@+id/b_orderMeal_cancel" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerHorizontal="true" android:layout_marginLeft="4dip" android:layout_marginRight="4dip" android:background="@drawable/button_selector" android:clickable="true" android:focusable="true" android:text="@string/b_cancel" android:textColor="#FFFFFF" /> </RelativeLayout> 

But only a cancel button appears, and this is because I used the “fill in parent element”, what is the solution?

+4
source share
2 answers

Try adding your code.

 <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/bg_selector" > <Button android:id="@+id/b_orderMeal_send" android:layout_toLeftOf="@+id/view" .... /> <View android:id="@+id/view" android:layout_height="1dp" android:layout_width="0dp" android:layout_centerHorizontal="true" /> <Button android:id="@+id/b_orderMeal_cancel" android:layout_toRightOf="@+id/view" ... /> </RelativeLayout> 
+5
source

try it

 <?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:layout_weight="1" android:id="@+id/button1" android:layout_width="0dp" android:layout_height="wrap_content" android:text="Button" /> <Button android:layout_weight="1" android:id="@+id/button2" android:layout_width="0dp" android:layout_height="wrap_content" android:text="Button" /> </LinearLayout> 
+13
source

All Articles