简体   繁体   中英

How to make 2 buttons stick together side by side?

Okay so I'm having this code in RelativeLayout with 2 buttons:

<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" >

    <Button
        android:id="@+id/xoay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="500dp"
        android:layout_marginLeft="160dp"
        android:layout_centerVertical="true"
        android:layout_gravity="center"
        android:text="Xoay" />
    <Button
        android:id="@+id/chon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="400dp"
        android:layout_marginLeft="160dp"
        android:layout_centerVertical="true"
        android:layout_gravity="center"
        android:text="Chọn" />

</RelativeLayout>

The problem is, users can only see 1 button at the same time because they're in the same place. I want these two buttons to stick together next to each other. Is there any way to do this in RelativeLayout? Thank you

I wrap the Button with linear layout so as to set the orientation to horizontal which can align the elements inside the layout side by side

<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" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:orientation="horizontal"
        android:gravity="center"
        android:layout_centerInParent="true">

        <Button
            android:id="@+id/xoay"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Xoay" />

        <Button
            android:id="@+id/chon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Chọn" />

    </LinearLayout>




</RelativeLayout>

By using a Horizontal Linear Layout you can easily achieve this .,

<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" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <Button
            android:id="@+id/chon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"

            android:layout_marginLeft="110dp"
            android:text="Chọn" />

        <Button
            android:id="@+id/xoay"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Xoay" />
    </LinearLayout>

</RelativeLayout>

I suggest you can try out the constraint layout if you have a complex layout to build.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM