为什么 RelativeLayout 在其高度设置为 WRAP_CONTENT 时一直弄乱其内部布局?

Why RelativeLayout keeps messing its inner layout while its height is set to WRAP_CONTENT?

我有一个 RelativeLayout,它环绕着一个 ImageView 和 2 个 TextView。这里的问题是当我将它的高度设置为 wrap_content 时,其中一个 TextViews 与另一个重叠,而它被设置为驻留在它下面但我将高度设置为特定的 dp(如 56dp)没关系.请参考下面的代码。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/selectableItemBackground"
    android:clickable="true"
    android:focusable="true">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/avatar"
        android:layout_width="36dp"
        android:layout_height="36dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_margin="10dp"
        android:scaleType="centerCrop"
        android:src="@drawable/new_profile_avatar"/>

    <com.imnumbers.newpkg.widget.CustomTextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/avatar"
        android:text="Hamed Momeni"/>

    <TextView
        android:id="@+id/number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/name"
        android:layout_toEndOf="@+id/avatar"
        android:layout_toRightOf="@+id/avatar"
        android:text="+989888898885"
        android:textSize="9sp"
        />

</RelativeLayout>

我还附上了截图来说明问题。

外观截图:

应该的截图:

只需将您的 RelativeLayout 身高更改为 "match_parent"android:layout_height="match_parent"

android:layout_centerVertical="true" for name, android:layout_below="@+id/name" for number and android:layout_height="wrap_content" for RelativeLayout 创建相互依赖并导致错误

建议的修复:

  1. name删除android:layout_centerVertical="true",您可以为name添加marginTop,使其向下移动一点,例如android:layout_marginTop="5dp";或
  2. RelativeLayout 设置固定高度,即 android:layout_height="36dp"