Как сделать так, чтобы 2 кнопки слипались рядом? - PullRequest
0 голосов
/ 02 апреля 2020

Хорошо, у меня есть этот код в RelativeLayout с 2 кнопками:

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

Проблема в том, что пользователи могут видеть только 1 кнопку одновременно, потому что они находятся в одном и том же месте. Я хочу, чтобы эти две кнопки слипались рядом друг с другом. Есть ли способ сделать это в RelativeLayout? Спасибо

Ответы [ 2 ]

0 голосов
/ 02 апреля 2020

Используя Горизонтальный линейный макет , вы легко можете достичь этого.

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

Я предлагаю вам попробовать макет ограничения , если у вас есть сложная планировка для сборки.

0 голосов
/ 02 апреля 2020

Я обертываю кнопку с линейным расположением, чтобы установить горизонтальную ориентацию, которая может выровнять элементы внутри расположения рядом

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