0

I have an horizontal linear layout with 3 child views.

I want to center the middle child horizontally and make its siblings 8dps to its sides.

Is this the clean way to do so?

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    android:orientation="horizontal">
  <include
      android:id="@+id/child1"
      layout="@layout/child1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center_horizontal|end"
      android:maxLines="1" />
  <include
      android:id="@+id/child2"
      layout="@layout/child2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center"
      android:layout_marginStart="@dimen/separator_horizontal_margin"
      android:layout_marginLeft="@dimen/separator_horizontal_margin"
      android:layout_marginEnd="@dimen/separator_horizontal_margin"
      android:layout_marginRight="@dimen/separator_horizontal_margin" />
  <include
      android:id="@+id/child3"
      layout="@layout/child3"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center_horizontal|start"
      android:maxLines="1" />
</LinearLayout>
Elad Benda
  • 35,076
  • 87
  • 265
  • 471

1 Answers1

0

User RelativeLayout instead

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="end">
    <include
        android:id="@+id/child1"
        layout="@layout/child1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toStartOf="@id/child2"
        android:layout_gravity="center_horizontal|end"
        android:maxLines="1" />
    <include
        android:id="@+id/child2"
        layout="@layout/child2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_centerHorizontal="true"
        android:layout_marginStart="@dimen/separator_horizontal_margin"
        android:layout_marginLeft="@dimen/separator_horizontal_margin"
        android:layout_marginEnd="@dimen/separator_horizontal_margin"
        android:layout_marginRight="@dimen/separator_horizontal_margin" />
    <include
        android:id="@+id/child3"
        layout="@layout/child3"
        android:layout_toEndOf="@+id/child2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|start"
        android:maxLines="1" />
</RelativeLayout>
Hossam Hassan
  • 795
  • 2
  • 13
  • 39