Android RelativeLayout 中的重心不像 LinearLayout 那样工作

Android Gravity Center in RelativeLayout Doesn't Work Like LinearLayout

对于RelativeLaout

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <TextView
        android:id="@+id/a_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:text="Sam"
        android:textColor="@color/black"
        android:textSize="30sp" />

    <TextView
        android:id="@+id/b_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:text="Kate"
        android:textColor="@color/black"
        android:textSize="30sp"
       android:layout_toEndOf="@id/a_textView"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show"
        android:textSize="30sp"
        android:layout_toEndOf="@id/b_textView"/>

</RelativeLayout>

结果:未完全居中

对于LinearLayout

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="center">    //<- here, child Views are the same

结果:完美居中

问题:为什么如此以及如何通过仅修改 RelativeLayout中的属性使RelativeLayoutLinearLayout中获得相同的结果?

您有不同的解决方案,但更改了子视图中的一些属性。

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    ..>

    <LinearLayout
       android:orientation="horizontal"
       android:gravity="center_horizontal">

        <TextView/>
        <TextView/>
        <Button/>

    </LinearLayout>
</RelativeLayout>
        

或:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    ..>

        <TextView/>
        <TextView/>
        <Button
          android:layout_alignBaseline="@id/a_textView">

</RelativeLayout>

使用layout_alignTop和layout_alignBottom

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">

<TextView
    android:id="@+id/a_textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:text="Sam"
    android:gravity="center"
    android:layout_alignBottom="@id/b_textView"
    android:layout_alignTop="@id/b_textView"
    android:textColor="@color/black"
    android:textSize="30sp" />

<TextView
    android:id="@+id/b_textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="16dp"
    android:text="Kate"
    android:gravity="center"
    android:textColor="@color/black"
    android:textSize="30sp"
    android:layout_alignTop="@id/button"
    android:layout_alignBottom="@id/button"
    android:layout_toEndOf="@id/a_textView"/>

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Show"
    android:gravity="center"
    android:textSize="30sp"
    android:layout_toEndOf="@id/b_textView"/>

</RelativeLayout>