图像未在布局预览中显示

Image not being displayed in layout preview

我正在制作一个测验应用程序,并希望这个灯泡图像始终存在。

我尝试在布局中添加一个 ImageView,让它在预览中显示, 但是当我 运行 应用程序时,图像不显示。

我尝试更改 XML 代码 app:srcCompat="@drawable/bulb"android:src="@drawable/bulb" (我的应用崩溃了)

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:background="@color/colorPrimaryDark"
    android:orientation="vertical" >

    <ProgressBar
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/progress_bar"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:indeterminate="false" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/progress_bar"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:id="@+id/score"
        android:textColor="@color/colorText"
        android:textStyle="bold"
        android:textSize="25sp"
        android:text="@string/initial_score"
        android:padding="10dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="50dp"
        android:id="@+id/question_text_view"
        android:gravity="center"
        android:padding="20dp"
        android:text="@string/question_1"
        android:textColor="@color/colorText"
        android:textStyle="bold"
        android:textSize="30sp" />

    **<ImageView
        android:id="@+id/bulbimage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="40dp"
        android:paddingTop="60dp"
        android:paddingRight="40dp"
        android:paddingBottom="50dp"
        app:srcCompat="@drawable/bulb"
        android:contentDescription=""/>**

    <LinearLayout
        android:layout_centerHorizontal="true"
        android:layout_above="@id/score"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="20dp"
        android:orientation="horizontal">

        <Button
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:id="@+id/true_button"
            android:text="@string/true_button"
            android:textColor="@color/colorText"
            android:textStyle="bold"
            style="@style/Base.Widget.AppCompat.Button.Borderless"
            android:background="@color/colorTrueButton"
            android:textSize="20sp" />

        <Button
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            style="@style/Base.Widget.AppCompat.Button.Borderless"
            android:id="@+id/false_button"
            android:text="@string/false_button"
            android:background="@color/colorFalseButton"
            android:textColor="@color/colorText"
            android:textStyle="bold"
            android:textSize="20sp"/>

    </LinearLayout>

</RelativeLayout>
  1. 检查图像是否在 drawable 文件夹中,而不是在 drawable-21 或其他任何地方..
  2. 尝试为图像添加高度和宽度(如 100dp)。
  3. 或者检查图片是否过大!!

如果代码中没有语法错误,(在main.java文件或activity.XML文件中)->

简单地减小正在使用的图像的大小。 (通过裁剪或压缩)

详细逻辑-> 位图的最大大小因设备而异,具体取决于可用的堆space。在具有 Google Play 的任何设备上,堆 space 所需的最小值是 16 MB。

您的应用程序的其余部分也在同一个堆中运行 space,因此整个堆不可用于单个位图。

此外,位图的大小会因颜色格式和高宽而异。

最好的策略是只将该屏幕尺寸所需的位图大小加载到内存中,不要再大了。