【发布时间】: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>
[在布局中看起来像这样]
【问题讨论】:
-
在 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