Правое выравнивание кнопки в линейном макете в Android с кнопкой рядом - PullRequest
1 голос
/ 14 мая 2019

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

Пожалуйста, помогите.

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


                <ImageButton
                    android:background="@null"
                    android:id="@+id/reply_button"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="20dp"
                    android:src="@drawable/reply"
                    android:layout_gravity="left"/>

                <Button
                    android:id="@+id/readmore_button"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:background="@null"
                    android:text="Read more.."
                    android:textAllCaps="false"
                    android:textColor="@android:color/white"
                    android:textSize="14sp" />
            </LinearLayout>

Here is the output for the reply

1 Ответ

1 голос
/ 15 мая 2019

Вы можете установить android:layout_weight="2" для своего контейнера и для каждого дочернего элемента установить android:layout_weight="1" и android:layout_width="0dp" следующим образом:

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

<ImageButton
    android:background="@null"
    android:id="@+id/button"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_marginBottom="20dp"
    android:layout_weight="1"
    android:layout_gravity="left"/>

<Button
    android:id="@+id/button2"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_gravity="right"
    android:background="@null"
    android:text="Read more.."
    android:textAllCaps="false"
    android:layout_weight="1"
    android:textColor="@android:color/white"
    android:textSize="14sp" />
</LinearLayout>

Но если вы уже разрабатывали некоторые приложения ios ранее, как следует из вашего именивам, возможно, будет удобнее работать с ConstarintLayout - одним макетом для всех размеров экрана (он очень похож на редактор перетаскивания в ios, я просто не могу вспомнить его имя)

Вот пример использования ConstraintLayout:

<Button
    android:id="@+id/button"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/button2"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/button2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="@+id/button"
    app:layout_constraintEnd_toStartOf="@+id/button"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/button" />
</android.support.constraint.ConstraintLayout>

Редактировать в соответствии с тем, что вы просили:

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

<Button
    android:id="@+id/button"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:text="left"
    android:layout_gravity="start"/>

<Button
    android:id="@+id/button2"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    android:text="Read more.." 
    android:layout_weight="1"
    android:textSize="14sp" />

<Button
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:text="right"
    android:layout_weight="1"/>
</LinearLayout>
...