为什么 RelativeLayout 不强制 layout_alignLeft 与 layout_centerInParent 视图?

Why is RelativeLayout not enforcing layout_alignLeft with layout_centerInParent view?

为什么(见鬼)RadioButton 左边缘没有与灰色方形左边缘正确对齐?

这是 RelativeLayout 的某种限制或错误,阻止将视图与以父视图为中心的另一个视图对齐吗?

最干净的解决方法是什么?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/holo_orange_dark">

    <View
        android:id="@+id/square"
        android:layout_width="200dip"
        android:layout_height="200dp"
        android:layout_centerInParent="true"
        android:background="@android:color/darker_gray"/>

    <RadioButton
        android:layout_alignLeft="@id/square"
        android:text="Radio button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_blue_light"/>

    <View
        android:layout_width="300dp"
        android:layout_height="20dp"
        android:layout_centerInParent="true"
        android:background="@android:color/holo_green_light"
        />

</RelativeLayout>

RadioButton 缺少元素 android:layout_centerInParent="true" 以使其保持在屏幕中央或使用 android:layout_centerHorizontal="true" 下面的代码是 centerInParent

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/holo_orange_dark">

  <View
      android:id="@+id/square"
      android:layout_width="200dip"
      android:layout_height="200dp"
      android:layout_centerInParent="true"
      android:background="@android:color/darker_gray"/>

  <RadioButton
      android:layout_alignLeft="@id/square"
      android:text="Radio button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerInParent="true"
      android:background="@android:color/holo_blue_light"/>

  <View
      android:layout_width="300dp"
      android:layout_height="20dp"
      android:layout_centerInParent="true"
      android:background="@android:color/holo_green_light"
      />

</RelativeLayout>

您需要将 ViewRadioButton 包装在 RelativeLayout 中。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_orange_dark">

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true">

    <View
        android:id="@+id/square"
        android:layout_width="200dip"
        android:layout_height="200dp"
        android:layout_centerInParent="true"
        android:background="@android:color/darker_gray"/>

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_blue_light"
        android:text="Radio button"/>
</RelativeLayout>

<View
    android:layout_width="300dp"
    android:layout_height="20dp"
    android:layout_centerInParent="true"
    android:background="@android:color/holo_green_light"
    />

由于相对布局宽度wrap_content

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_orange_dark">

<View
    android:id="@+id/square"
    android:layout_width="200dip"
    android:layout_height="200dp"
    android:layout_centerInParent="true"
    android:background="@android:color/darker_gray"/>

<RadioButton
    android:layout_alignLeft="@+id/square"
    android:layout_alignStart="@+id/square"
    android:text="Radio button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/holo_blue_light"/>

<View
    android:layout_width="300dp"
    android:layout_height="20dp"
    android:background="@android:color/holo_green_light"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />