【问题标题】:Why is my image showing in preview but not in the app simulator?为什么我的图像显示在预览中,但没有显示在应用模拟器中?
【发布时间】:2019-02-04 18:05:26
【问题描述】:

我正在努力解决 ImageView 标记的问题。它似乎在预览中从可绘制文件夹中加载 PNG,但是当我通过模拟器在手机上运行应用程序时,图像没有显示。

这是我的图片的 XML 布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000"
    tools:context="org.tensorflow.demo.CameraActivity" >

    <Button
        android:id="@+id/calButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start/Stop" />

    <Button
        android:id="@+id/clearButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="110px"
        android:text="Clear" />


    <ImageView
        android:id="@+id/cView"
        android:layout_width="50dp"
        android:layout_height="46dp"
        android:layout_marginTop="240px"
        android:src="@drawable/record_circle" />

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="120dp"
        android:layout_height="62dp"
        android:layout_gravity="bottom|end"
        android:background="@android:drawable/btn_dropdown"
        android:spinnerMode="dropdown" />

</FrameLayout>

这是我的活动中用于加载图像的代码:

ImageView circleView = (ImageView) findViewById(R.id.cView);
circleView.setImageResource(0);

Drawable circleDraw = getResources().getDrawable(R.drawable.record_circle);
circleDraw = (circleDraw);
circleView.setImageDrawable(circleDraw);

谢谢!

**我尝试在 android:src="@drawable..." 属性中同时使用 src 和背景

【问题讨论】:

  • 移除背景试试这个android:src="@drawable/record_circle" 也不需要通过代码加载
  • 谢谢!我实际上早一点尝试过,它似乎也不起作用。代码会不会把事情搞砸了?
  • 去掉代码后你试过了吗?
  • circleDraw = (circleDraw); - 这行应该做什么?
  • 给出完整的布局代码

标签: android android-studio android-layout android-imageview


【解决方案1】:

试试这个:

<ImageView
android:id="@+id/cView"
android:layout_width="50dp"
android:layout_height="46dp"
android:layout_marginTop="240px"
app:srcCompat="@drawable/record_circle" />

这是应用命名空间导入的示例:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

请注意,这是最顶层的布局,所以它可能是“LinearLayout”或您使用的任何内容

【讨论】:

  • 这给了我一个资源链接失败的错误。我很确定文件路径是正确的。
  • 您可能需要在 xml 的顶部元素中包含应用命名空间。更新了我的答案以表明我的意思
  • 谢谢 - 我试过这个,但它导致了同样的错误。它说命名空间声明也从未使用过
  • 没问题。对不起,没有帮助。你能用完整的 XML 更新你的答案吗?
猜你喜欢
  • 2020-11-15
  • 1970-01-01
  • 1970-01-01
  • 2021-03-31
  • 1970-01-01
  • 2019-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多