虚拟ImageView不显示图像

时间:2015-04-09 16:54:12

标签: android imageview

它在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>

2 个答案:

答案 0 :(得分:2)

你的课程是否扩展了活动?尝试扩展AppCompatActivity。我有同样的问题,这个解决方案修复了它。

答案 1 :(得分:1)

除了我的评论,我还添加了一些可能的灵魂。图像没有显示,这很奇怪。它是否在xml gui设计视图的imageview中显示?

无论如何,这里有两种可能的解决方案: 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可能会降低图像的质量。尝试搞乱其他比例类型