two elements in linear layout are packed to center instead of to the sides

94 views Asked by At

I have this layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
  <com.google.android.libraries.onegoogle.account.particle.AccountParticle
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/part_a"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center_vertical|start" />
  <ImageView
      android:id="@+id/part_b"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginEnd="14dp"
      android:layout_marginRight="14dp"
      android:layout_gravity="center_vertical|end"
      app:srcCompat="@drawable/my_image" />
</LinearLayout>

I would expect part_a to be aligned to the right and part_b aligned to the left. (Thanks to android:layout_gravity).

Why do I see them packed closed to each other?

enter image description here

1

There are 1 answers

0
Nima Ganji On

Use ConstraintLayout

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <com.google.android.libraries.onegoogle.account.particle.AccountParticle
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/part_a"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>
    <ImageView
        android:id="@+id/part_b"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="14dp"
        android:layout_marginRight="14dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:srcCompat="@drawable/my_image" />
</android.support.constraint.ConstraintLayout>