当前位置: 首页 > 知识库问答 >
问题:

android studio中的图像视图以xml布局显示,但未在应用程序中显示

韩阳云
2023-03-14

android studio中的图像视图以xml布局显示,但在运行后的应用程序中没有显示。我尝试了所有可用的解决方案,但没有找到解决问题的方法。

我正在制作一个简单的meme共享应用程序,其中有两个按钮“共享”和“下一个”,单击“下一个”按钮后,出现了另一个meme。我使用了一个随机的meme api。我没有完成应用程序(没有为“下一个”按钮和“共享”按钮添加功能),只是为了检查是否显示了meme图像。我成功运行了代码。它安装在我的手机上,按钮正在显示,但图像并非仅显示白色背景

主活动.kt

package com.example.memeshare

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.JsonObjectRequest
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import com.bumptech.glide.Glide

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    loadMeme()
}

private fun loadMeme(){

  var memeImageview = findViewById<ImageView>(R.id.imageView)
// ...

// Instantiate the RequestQueue.
    val queue = Volley.newRequestQueue(this)
    val url = "https://i.redd.it/bi9tnlxmqlr71.jpg"


    val jsonObjectRequest = JsonObjectRequest(
        Request.Method.GET, url,null,
        { response ->

          val url = response.getString("url")
            Glide.with(this).load(url).into(memeImageview)
        },
        {  },
    )

// Add the request to the RequestQueue.
    queue.add(jsonObjectRequest)
}
fun shareMeme(view: View) {

}

fun nextMeme(view: View) {

}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    tools:srcCompat="@tools:sample/avatars" />

<Button
    android:id="@+id/shareButton"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="@string/share"
    android:padding="32dp"
    app:layout_constraintRight_toRightOf="@id/guideline2"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    android:onClick="shareMeme"/>



<Button
    android:id="@+id/nextButton"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:width="0dp"
    android:padding="32dp"
    android:text="@string/next"
    app:layout_constraintLeft_toLeftOf="@id/guideline2"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    android:onClick="nextMeme"/>

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/guideline2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintGuide_percent="0.5"/>




</androidx.constraintlayout.widget.ConstraintLayout>

[布局上是这样的]

(https://i.stack.imgur.com/CarzW.jpg)

共有2个答案

董建德
2023-03-14

您的错误是使用工具:srcCompat。因此,当您使用工具时,您的应用程序中不会显示任何内容您必须使用android: src="绘图/头像"

闻人思聪
2023-03-14

您需要设置

android:layout_width="wrap_content"
android:layout_height="wrap_content

您需要向imageview添加源或背景。

android:background="@drawable/image"
android:src="@drawable/image"
 类似资料:
  • 它将我带到图库以选择图像,但未显示在应用程序中,当单击上传按钮时,它只是一个空白图像视图 < li >我的Java代码 < li>XML代码 运行时显示此错误 java.lang.IllegalArgumentException:uri不能为空 以下两处给出错误的行显示了uri错误

  • 问题内容: 我想以特定尺寸在Android应用程序中显示图片。我该怎么做?请指导我?还有一件事,我想从SD卡中获得该图像。所以请帮帮我。 提前致谢。 问题答案: 首先,您需要创建一个imageview。 创建布局参数以在布局上添加imageview 然后获取您的图像路径 在ImageView上设置图像 获取您要添加的布局 将视图添加到布局

  • 我的程序在Eclipse中运行完美,但是当我导出它时,图像不显示出来。 我的设置看起来像 我参考标志。来自MenuMain的png。java,我尝试了很多方法; 前3个在Eclipse中工作,但都不在Jar中工作。我检查了Jar文件,res文件夹在那里,所有的图像也是如此。我还尝试编辑MANIFEST. MF并添加了类路径: 但它仍然不起作用。我在运行Jar时没有发现任何错误,其他所有内容都显示在

  • 请帮我做这件事。我已经尝试了很多东西,甚至这里的反应如何显示图像上的文本,但没有得到所需的输出,如该页面的第一个图像所示。我试图用文本覆盖显示图像。我得到了一个用惰性适配器加载图像的代码。但是显示的图像在两侧都被填充。这是我得到的 这是我的代码: 平铺布局xml 网格布局 我正在给瓷砖充气

  • ![我已经以线性布局(下面给出的代码)对其进行了编码,但想知道如何使用相对布局进行相同的操作,或者这只是一个考虑到布局的坏主意? 线性布局 - ][1] 使用相对布局时,我的ImageView不会显示在屏幕上: