首页 > 解决方案 > ImageView 在 RelativeLayout 内不显示全高

问题描述

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

    <ImageView
        android:id="@+id/ImgView"
        android:layout_centerInParent="true"
        android:scaleType="fitXY"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>


</RelativeLayout>

图像实际比例为 360/640,但目前显示为 1:1,图像高度显示不正确。我试图将图像视图 layout_height 设置为 wrap_content 但它也不起作用。如何正确显示图像的高度?

在此处输入图像描述

标签: androidimageviewandroid-relativelayout

解决方案


我更喜欢使用如下所示的约束布局而不是相对布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/Gray"
    android:type="radial"
   >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/logo" />



</androidx.constraintlayout.widget.ConstraintLayout>

推荐阅读