如何在布局中居中,并设置其兄弟姐妹紧靠其布局?

我有3个子视图的水平线性布局。

我想使中间孩子水平居中,并将其兄弟姐妹的侧面朝外8dps。

这是干净的方法吗?

<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>
gaolvsuan 回答:如何在布局中居中,并设置其兄弟姐妹紧靠其布局?

改为用户RelativeLayout

<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>
本文链接:https://www.f2er.com/3086229.html

大家都在问