In android, how should I put 2 buttons on the same line with the same width and height?

In android, how should I put 2 buttons on the same line with the same width and height? Which layout should I use?

+4
source share
2 answers

layout_weight is responsible for adjusting components in equal or different proportions.

Use this:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <Button android:text="@+id/Button01" android:id="@+id/Button01" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <Button android:text="@+id/Button02" android:id="@+id/Button02" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout> 
+11
source

You can use RelativeLayout to place more than one button on multiple lines:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/textView1" android:layout_marginLeft="22dp" android:layout_marginTop="43dp" android:text="Button" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button1" android:layout_alignBottom="@+id/button1" android:layout_alignParentRight="true" android:layout_marginRight="36dp" android:text="Button" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/button1" android:layout_below="@+id/button1" android:layout_marginTop="56dp" android:text="Button" /> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button3" android:layout_alignBottom="@+id/button3" android:layout_alignRight="@+id/button2" android:text="Button" /> </RelativeLayout> 
0
source

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


All Articles