为什么我的ImageView不显示图像?

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

尝试使用

ImageView
显示图像,但未显示。 源图像保存在
drawable
目录中。

<?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:id="@+id/constrainLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="centerInside"
        android:visibility="visible"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.485"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="ContentDescription"
        tools:srcCompat="@drawable/nature"/>
</androidx.constraintlayout.widget.ConstraintLayout>
android imageview
4个回答
3
投票

tools:srcCompat="@drawable/nature"
用于在设计时显示图像。当您需要在运行时查看它时,您应该在您的
android:src="@drawable/nature"
中添加
ImageView

<?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:id="@+id/constrainLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="centerInside"
        android:visibility="visible"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.485"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:src="@drawable/nature"
        tools:ignore="ContentDescription"
        tools:srcCompat="@drawable/nature"/>
</androidx.constraintlayout.widget.ConstraintLayout>

2
投票

而不是使用

        tools:srcCompat="@drawable/nature"

使用

        android:src="@drawable/nature"

1
投票

更换即可

tools:srcCompat="@drawable/nature"

android:src="@drawable/nature"


0
投票

仅开发时间属性 ->

tools:

生产级别(运行时)属性 ->

android:
app:

运行时工具属性将被删除。所以你必须用

android:src
替换该属性。

tools:srcCompat="@drawable/nature"
->
android:src="@drawable/nature"

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