【问题标题】:Image view in android studio is showing in xml layout but not showing in appandroid studio 中的图像视图显示在 xml 布局中,但未显示在应用程序中
【发布时间】:2021-10-06 12:35:53
【问题描述】:

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

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

MainActivity.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)

【问题讨论】:

  • 在 ImageView 中显示加载图像的代码。请注意,tools:srcCompat="@tools:sample/avatars" 仅在 Android Studio 中有效,但在运行应用时无效。
  • @MarioHuizinga 感谢您的回答????我正在制作一个简单的 meme 共享应用程序,其中将有两个按钮共享和下一个,单击下一个按钮后会出现另一个 meme 我使用了一个随机 meme api 我还没有完成应用程序(没有为下一个按钮和共享按钮添加功能)只是为了检查 meme 图像是否显示,我成功运行了我的代码,它安装在我的手机共享上,并且显示了下一步按钮,但图像没有显示,只有白色背景。我用代码更新了问题,请帮忙。
  • 尝试使问题中的代码更具可读性:编辑问题并按照右侧的说明进行操作。将您的代码标记为代码,没有不必要的空行,并带有缩进。还要检查这个答案:stackoverflow.com/a/35306315/7493938
  • 你从API成功获取图片URL了吗?
  • @MohakShah 是的 API 正在加载图像。我错了,回答了我的问题,你可以检查它stackoverflow.com/a/69477022/16655199

标签: android xml


【解决方案1】:

你需要设置

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

并且你需要为 imageview 添加源或背景。

android:background="@drawable/image"
android:src="@drawable/image"

【讨论】:

  • @a_local_nobody 我以为tools:srcCompat 正在设置图像。
  • @a_local_nobody 感谢您的澄清。一票否决,但仍有一些东西要学。 :-)
  • @a_local_nobody 我更新了答案。现在我认为它很好。
  • @a_local_nobody 我用代码更新了我的问题,请帮忙。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    ?感谢所有回答我的问题的人,他们费了很大劲才找到为什么我的图像视图没有加载我弄错了,实际上我做错了什么

    val url = "https://i.redd.it/bi9tnlxmqlr71.jpg"
    

    我把图片链接放在val url而不是API链接 "https://meme-api.herokuapp.com/gimme"

    所以正确的是:

    val url ="https://meme-api.herokuapp.com/gimme"
    

    这次更正后,我的表情包正在加载。

    另外,"tools:srcCompat="@tools:sample/avatars" 仅用于调试,在您运行应用程序时它不会做任何事情。

    正确的代码,

    MainActivity.kt

    package com.example.memeshare
    import android.graphics.drawable.Drawable
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import android.view.View
    import android.widget.ImageView
    import android.widget.TextView
    import android.widget.Toast
    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
    import com.bumptech.glide.load.DataSource
    import com.bumptech.glide.load.engine.GlideException
    import com.bumptech.glide.request.RequestListener
    import com.bumptech.glide.request.target.Target
    
    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://meme-api.herokuapp.com/gimme"
    
    // Request a string response from the provided URL.
           val jsonObjectRequest = JsonObjectRequest(
                   Request.Method.GET, url,null,
                   {
                       response ->
                       val url = response.getString("url")
                       Glide.with(this).load(url).into(memeImageview)
                   },
                   {
                     Toast.makeText(this,"Something went wrong",Toast.LENGTH_LONG).show()
                   }
                   )
    
    
           queue.add(jsonObjectRequest)
    
    
    
       }
    
        fun nextMeme(view: View) {
            loadMeme()
        }
    
        fun shareMeme(view: View) {}
    
    
    // Add the request to the RequestQueue.
    
        }
    

    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="0dp"
            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="190dp"
            android:layout_height="wrap_content"
            android:text="@string/share"
            android:padding="32dp"
            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="190dp"
            android:padding="32dp"
            android:text="@string/next"
            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>
    
    
    
    
    
    
    
    
    
    
     
         
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-05
      • 1970-01-01
      • 2019-07-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多