虚拟 ImageView 不显示图像

Dummy ImageView not showing image

它在 Android Studio 预览中显示图像,但当我在我的设备上 运行 时没有显示任何内容。为什么会发生这种情况的任何想法?我的代码有什么问题?

<?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">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/myimage" />

</RelativeLayout>

除了我的评论之外,我还添加了一些可能的想法。很奇怪图像还没有显示出来。它是否显示在 xml gui 设计视图的图像视图中?

无论如何,这里有两种可能的解决方案: xml:

<ImageView

            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/myimage" />

此外,您的 imageview 还没有分配 ID,这看起来很奇怪,我的第二个想法需要这样的东西:

<ImageView
               android:id="@+id/Image_ID"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
            />

Java 边:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); //what ever the name of the activity you are using 

        Image=     (ImageView)findViewById(Image_ID);
        Image.setImageResource(Image_ID);
}

希望这对您有所帮助!

编辑

<ImageView
            android:scaleType="fitXY"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/myimage" />

注意 fitXY 可能会降低图像质量。尝试弄乱其他音阶类型

您的 class 扩展 Activity 了吗?尝试扩展 AppCompatActivity。我遇到了同样的问题,此解决方案解决了它。