【问题标题】:Dummy ImageView not showing image虚拟 ImageView 不显示图像
【发布时间】:2017-01-23 17:06:04
【问题描述】:

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

【问题讨论】:

  • 你试过清理和重建你的项目吗?
  • 现在试过了,没用。图像仍然不会显示。
  • 愚蠢的问题,但可以肯定的是,您实际上是在使用布局,对吗?在 java 中使用 setContentView,我的意思是 Activity。
  • 是的,请检查Activity 中的代码,您是否使用了正确的布局setContentView。
  • 是的。我试图在这个 ImageView 下添加一个 TextView。 TextView 显示,但 ImageView 不可见。

标签: android imageview


【解决方案1】:

您的课程是否扩展了 Activity?尝试扩展 AppCompatActivity。我有同样的问题,这个解决方案解决了它。

【讨论】:

  • 这个解决方案救了我。
【解决方案2】:

除了我的评论之外,我还添加了一些可能的灵魂。很奇怪,图像还没有显示。它是否显示在 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 可能会降低图像质量。尝试弄乱其他比例类型

【讨论】:

  • 好的,我尝试更改图像并且它有效。现在的问题是图像有什么问题?
  • 您能提供图片的详细信息吗?我只能从头顶推测图像太大了?如果图像太大而无法显示,您可以使用paint.net 之类的程序将图像缩小。或查看我的编辑
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多