将ImageView放在TextView下面

问题描述 投票:0回答:2

我有一个RelativeLayoutTextViewImageView看起来像这样:

enter image description here

橙色是ImageView的背景颜色,用于说明图像本身的起点和ImageView的起点之间有多少空间。如何让我的图像显示在我的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>
android android-layout android-relativelayout
2个回答
1
投票

你可以通过这种方式使用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>

这是我的结果的屏幕截图

enter image description here

另外,由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>

0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.