将 ImageView 放在 TextView 下方

Placing an ImageView below a TextView

我有一个 RelativeLayout,一个 TextView 和一个 ImageView,看起来像这样:

橙色是 ImageView 的背景颜色,用于说明图像本身的开始与 ImageView 的开始之间有多少 space。如何让我的图片显示在 ImageView 的顶部,以便标题直接位于图片上方,中间没有橙色?

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Recyclerview -->
    <com.sometimestwo.moxie.MultiClickRecyclerView
        android:id="@+id/recycler_zoomie_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- Container for title and image-->
    <RelativeLayout
        android:id="@+id/hover_view_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center|top"
        android:visibility="gone">

      <!-- Title -->
        <TextView
            android:id="@+id/hover_view_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorBgBlackTint"
            android:gravity="center"
            android:textColor="@color/colorWhite"
            android:visibility="visible" />

     <!-- Imageview that holds dog picture -->
        <ImageView
            android:id="@+id/hover_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/hover_view_title"
            android:background="@color/colorDarkThemeAccent"
            android:visibility="visible" />
    </RelativeLayout>

</FrameLayout>

这里有几个选项,我真的建议使用 ConstraintLayout,但这确实是另一个问题。为了使您的布局按预期工作,我建议添加 scaleType="fit_start"。这将缩放图像,使其适合您的 ImageView 并将其与 top/left 对齐(在 rtl 布局中)。设置 adjustViewBounds="true" 很可能也有效,除非您需要图像在所有情况下都那么高。

你可以使用 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="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="13dp"
        android:text="TextView"
        android:textSize="24sp"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="216dp"
        android:layout_height="217dp"
        android:layout_below="@+id/textView"
        android:scaleType="fitStart"
        android:adjustViewBounds="true"
        android:layout_centerHorizontal="true"
        android:background="@drawable/ic_launcher_background" />
</RelativeLayout>

这是我的结果截图

还有,通过LinearLayout这样的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <TextView
        android:layout_gravity="center"
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="13dp"
        android:text="TextView"
        android:textSize="24sp"
        android:textStyle="bold" />

    <ImageView
        android:layout_gravity="center"
        android:id="@+id/imageView2"
        android:layout_width="216dp"
        android:layout_height="217dp"
        android:background="@drawable/ic_launcher_background" />
</LinearLayout>