Подтвердить что ты не робот

Выравнивание двух кнопок влево и вправо на Android

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

Код макета XML:

<?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="vertical"
    android:background="@android:color/white"
    android:gravity="bottom" >


    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/black" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="prev"
            android:layout_alignParentRight="true" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="next" />

    </LinearLayout>

</LinearLayout>

Фактический выход:

Ожидаемый результат:

Как я могу это исправить?

4b9b3361

Ответ 1

Используйте RelativeLayout. Там вы можете установить android:layout_alignParentLeft и android:layout_alignParentRight. Это должно сработать для вас:

<?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="vertical"
    android:background="@android:color/white"
    android:gravity="bottom" >


    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/black" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="prev"
            android:layout_alignParentLeft="true" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="next"
            android:layout_alignParentRight="true"/>

    </RelativeLayout>

</LinearLayout>

Ответ 2

С линейной компоновкой

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1">

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Prev"/>
        </LinearLayout>


        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Next"/>

</LinearLayout>

С относительной компоновкой

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Prev"
            android:layout_alignParentLeft="true"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Next"
            android:layout_alignParentRight="true"/>
</RelativeLayout>

Ответ 3

Используйте Относительную компоновку в LinearLayout;

Также добавьте android:layout_alignParentLeft="true" в "prev" Button

и android:layout_alignParentRight="true" до "next" Button

<?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:background="@android:color/white"
    android:gravity="bottom"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/black" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"    <---- ADD this prop
            android:text="prev" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"    <----- ADD this prop
            android:text="next" />
    </RelativeLayout>

</LinearLayout>

Ответ 4

Что-то вроде этого:

<?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="vertical" >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="right" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="Button" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@+id/button1"
            android:orientation="vertical" >

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button" />
        </LinearLayout>
    </RelativeLayout>

</LinearLayout>

Идея состоит в том, чтобы создать RelativeLayout в качестве контейнера и поместить первую кнопку, а затем придерживать ее справа от родительского представления. После этого добавьте еще одну кнопку внутри LinearLayout и установите LinearLayout в левую часть первой кнопки.

Вот результат.

enter image description here

Ответ 5

ur layout xml должен быть таким, как показано ниже

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="bottom"
    android:background="#fff">


 <RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#000"
    android:layout_gravity="bottom" >
    <Button      
       android:id="@+id/button1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentLeft="true"
       android:text="Pre" />

   <Button
      android:id="@+id/button2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentRight="true"
      android:text="Next" />
</RelativeLayout>
  </RelativeLayout>

Ответ 6

Используйте RelativeLayout вместо LinearLayout, вы можете добавить теги android:layout_alignParentRight="true" и android:layout_alignParentLeft="true" для каждой кнопки.

Ответ 7

Вы можете использовать RelativeLayout следующим образом:

<?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:background="@android:color/white"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/black"
        android:gravity="bottom"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:text="prev" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="next" />
    </RelativeLayout>

</LinearLayout>

(Я использовал серый вместо белого для снимка экрана) LAYOUT

Ответ 8

Используйте FrameLayout и добавьте атрибут layout_gravity в каждую из кнопок

Ответ 9

Используйте margin_Start и End для разделения кнопок в макете

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
    >

    <TextView android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="-:Linear Layout:-"
              android:textSize="35sp"
              android:layout_gravity="center"
              android:layout_marginTop="250dp"
              tools:ignore="HardcodedText"/>

        <LinearLayout
                android:id="@+id/linear2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_marginTop="90dp">

        <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Relative"
                tools:ignore="ButtonStyle,HardcodedText"/>

        <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="200dp"
                android:text="Constraint"
                tools:ignore="ButtonStyle"
        />
        </LinearLayout>
    </LinearLayout>
    [enter image description here][1]


      [1]: https://i.stack.imgur.com/NMYj2.png

Ответ 10

Для лучшей производительности:

Лучше всего поставить два вида влево и вправо и один вид между линейным макетом, а не относительным расположением. Используйте приведенный ниже код, он также удалит вложение представлений... Это будет хорошо в плане производительности:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical">

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="prev" />

        <TextView android:text="Hi its working"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="next" />

    </LinearLayout>

enter image description here

Ответ 11

Сделайте что-то подобное, это, безусловно, поможет вам

<?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                      android:orientation="vertical"
                      android:layout_width="match_parent"
                      android:layout_height="match_parent"
        >
            <TextView android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="@string/Linear"

                      android:textSize="35dp"
                      android:layout_margin="90dp"

            />

            <LinearLayout
                    android:id="@+id/linearLayout1"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/black">

                <Button
                        android:id="@+id/button1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Relative"
                        />

                <Button
                        android:id="@+id/button2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="200dp"
                        android:text="Constraint" />

            </LinearLayout>

        </LinearLayout>