layout:below 无法在 CardView 内的 RelativeLayout 中工作

layout:below not working in RelativeLayout inside CardView

我在 CardView 中有一个 RelativeLayout,其中包含一个 ImageView 和两个 TextView。一个 TextView 必须居中,所以它被放置在 ImageView 之上。另一个 TextView 必须低于该 TextView,但出于某种我不知道的原因,它位于第一个 TextView 之上。我设法通过添加第二个 RelativeLayout 并将两个 TextView 放入其中来正确设置它,现在它们可以正确显示。但我很好奇为什么如果不添加第二个 RelativeLayout,第二个 TextView 不会出现在第一个下方。有人可以解释我吗?谢谢。

布局:

这种方式可行:

<?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"
xmlns:app="http://schemas.android.com/apk/res-auto">

<android.support.v7.widget.CardView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:elevation="10dp"
    app:cardUseCompatPadding="true"
    android:id="@+id/cv">

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

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/image"
            android:layout_alignParentTop="true"
            android:adjustViewBounds="true"
            android:src="@drawable/ic_launcher"/>

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

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/name"
                android:gravity="center"
                android:textStyle="bold"
                android:textSize="40sp"
                android:shadowColor="#ffffff"
                android:shadowDx="0"
                android:shadowDy="0"
                android:shadowRadius="6"
                android:textColor="@color/black"
                android:text="I am the name"
                android:layout_centerInParent="true" />

            <TextView
                android:id="@+id/type"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/name"
                android:gravity="center"
                android:layout_centerHorizontal="true"
                android:shadowColor="#ffffff"
                android:shadowDx="0"
                android:shadowDy="0"
                android:shadowRadius="6"
                android:text="I am the type"
                android:textColor="@color/red"
                android:textSize="25sp"
                android:textStyle="bold" />

        </RelativeLayout>

    </RelativeLayout>
</android.support.v7.widget.CardView>


</RelativeLayout>

这不是:

<?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"
xmlns:app="http://schemas.android.com/apk/res-auto">

<android.support.v7.widget.CardView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:elevation="10dp"
    app:cardUseCompatPadding="true"
    android:id="@+id/cv">

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

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/image"
            android:layout_alignParentTop="true"
            android:adjustViewBounds="true"
            android:src="@drawable/ic_launcher"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/name"
                android:gravity="center"
                android:textStyle="bold"
                android:textSize="40sp"
                android:shadowColor="#ffffff"
                android:shadowDx="0"
                android:shadowDy="0"
                android:shadowRadius="6"
                android:textColor="@color/black"
                android:text="I am the name"
                android:layout_centerInParent="true" />

            <TextView
                android:id="@+id/type"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/name"
                android:gravity="center"
                android:layout_centerHorizontal="true"
                android:shadowColor="#ffffff"
                android:shadowDx="0"
                android:shadowDy="0"
                android:shadowRadius="6"
                android:text="I am the type"
                android:textColor="@color/red"
                android:textSize="25sp"
                android:textStyle="bold" />

    </RelativeLayout>
</android.support.v7.widget.CardView>


</RelativeLayout>

这是因为 ImageView 的宽度为 match_parent。如果你想拥有那种形式的图像,你可以改为设置相对布局的背景。您可以为 CardView 和任何其他设置设置高度。下面是代码。

    <?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"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:elevation="10dp"
        app:cardUseCompatPadding="true"
        android:id="@+id/cv">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@mipmap/ic_launcher">



            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/name"
                android:gravity="center"
                android:textStyle="bold"
                android:textSize="40sp"
                android:shadowColor="#ffffff"
                android:shadowDx="0"
                android:shadowDy="0"
                android:shadowRadius="6"
                android:textColor="#000"
                android:text="I am the name"
                android:layout_centerInParent="true" />

            <TextView
                android:id="@+id/type"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/name"
                android:gravity="center"
                android:layout_centerHorizontal="true"
                android:shadowColor="#ffffff"
                android:shadowDx="0"
                android:shadowDy="0"
                android:shadowRadius="6"
                android:text="I am the type"
                android:textColor="#000"
                android:textSize="25sp"
                android:textStyle="bold" />

        </RelativeLayout>
    </android.support.v7.widget.CardView>


</RelativeLayout>

在其他情况下,如果您想在布局中使用 ImageView,可以将 RelativeLayout 宽度设置为 match_parent,将 ImageView 宽度设置为 wrap_content

如果您在未匹配项中看到 XML,您会发现 ImageView android:adjustViewBounds="true",其中此 ImageView 将设置为调整其边界以保持其可绘制对象的纵横比。

在上述情况下,请尝试将 LinearLayout 与 Orientation Vertical 用于 TextView 和 RelativeLayout 用于(LinearLayout 和 ImageView)。

constraintlayout 布局始终是 Ultimate Powerfull 布局。