установить три кнопки в строке (по горизонтали) андроид студии с LinearLayout - PullRequest
0 голосов
/ 21 октября 2019

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="start"
        android:gravity="start">

        <Button
            android:id="@+id/dialog_positive_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Set"
            android:background="#dde5ad" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="end"
        android:gravity="end">

        <Button
            android:id="@+id/dialog_negative_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="No"
            android:background="#dde5ad" />
    </LinearLayout>

Мне нужен точно центр этих двух.

Ответы [ 5 ]

1 голос
/ 21 октября 2019

использовать layout_weight

<?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:layout_weight="3"
    android:orientation="horizontal">

    <Button
        android:id="@+id/dialog_positive_btn"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#dde5ad"
        android:text="Set" />

    <Button
        android:id="@+id/dialog_neutral_btn"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="#dde5ad"
        android:layout_weight="1"
        android:text="your text" />

    <Button
        android:id="@+id/dialog_negative_btn"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="#dde5ad"
        android:layout_weight="1"
        android:text="No" />

</LinearLayout>
0 голосов
/ 21 октября 2019

Используйте это -

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="start"
        android:gravity="start">

        <Button
            android:id="@+id/dialog_positive_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Set"
            android:background="#dde5ad"
            />
    </LinearLayout>
 <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="center"
        android:gravity="center">

        <Button
            android:id="@+id/dialog_negative_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="No"
            android:background="#dde5ad"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="end"
        android:gravity="end">

        <Button
            android:id="@+id/dialog_negative_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="No"
            android:background="#dde5ad"
            />
    </LinearLayout>
    </LinearLayout>
0 голосов
/ 21 октября 2019

Вы должны попытаться установить android: layout_weight = "1" для каждой кнопки, каждая из которых занимает 1/3 доступной ширины, чтобы они были равномерно распределены по горизонтали. Посмотрите макеты ниже, у вас есть 2 варианта для получения похожих результатов, один использует gravity , а другой layout_weight . Окончательный результат аналогичен, но предпочтительным является вариант с layout_weight = "1" :

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

        <Button
            android:id="@+id/dialog_positive_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Set"
            android:background="#dde5ad"
            />

        <Button
            android:layout_marginStart="20dp"
            android:id="@+id/center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Center"
            android:background="#dde5ad"
            />


        <Button
            android:layout_marginStart="20dp"
            android:id="@+id/dialog_negative_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="No"
            android:background="#dde5ad"
            />
    </LinearLayout>

Или у вас есть второй вариант:

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

        <Button
            android:id="@+id/dialog_positive_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Set"
            android:background="#dde5ad"
            />

        <Button
            android:layout_weight="1"
            android:layout_marginStart="20dp"
            android:id="@+id/center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Center"
            android:background="#dde5ad"
            />


        <Button
            android:layout_weight="1"
            android:layout_marginStart="20dp"
            android:id="@+id/dialog_negative_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="No"
            android:background="#dde5ad"
            />
    </LinearLayout>
0 голосов
/ 21 октября 2019

Вы можете избавиться от ненужных линейных макетов. Если вам нужно добавить только три кнопки подряд, вы можете сделать это только на горизонтальном LinearLayout, например так:

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
    <Button
        android:id="@+id/dialog_positive_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Set"
        android:background="#dde5ad"
        />

    <!-- this is your new Button -->
     <Button
        android:id="@+id/new_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Set"
        android:background="#dde5ad"
        />

    <Button
        android:id="@+id/dialog_negative_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="No"
        android:background="#dde5ad"
        />
</LinearLayout>

Также обратите внимание, что раскладка раскроя лиенара может повлиять на вашу производительность. Если вам нужно сделать сложный макет, попробуйте с макетом ограничений

Надеюсь, это поможет, удачного кодирования!

0 голосов
/ 21 октября 2019

Вам нужно использовать layout_weight & layout_gravity для этого:

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="start"
        android:layout_weight="1"
        android:gravity="start"
        android:orientation="vertical">

        <Button
            android:id="@+id/dialog_positive_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#dde5ad"
            android:text="Set" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="vertical">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#dde5ad"
            android:text="Set" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="end"
        android:gravity="end"
        android:orientation="vertical">

        <Button
            android:id="@+id/dialog_negative_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#dde5ad"
            android:text="No" />
    </LinearLayout>
</LinearLayout>
...