Android:动态设置ImageView?
我正在尝试下载图像,并动态创建 ImageView 并将其添加到我的布局中,但它不会显示图像。这是我的代码:
ImageView image = new ImageView(this);
LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
image.setLayoutParams(vp);
image.setScaleType(ImageView.ScaleType.CENTER_CROP);
image.setMaxHeight(50);
image.setMaxWidth(50);
image.setImageDrawable(avatar);
theLayout.addView(image);
也许我需要在添加 ImageView 后刷新布局?如何“刷新”?
I'm trying to download an image, and create an ImageView dynamically and add it to my layout, but it won't display the image. Here is my code:
ImageView image = new ImageView(this);
LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
image.setLayoutParams(vp);
image.setScaleType(ImageView.ScaleType.CENTER_CROP);
image.setMaxHeight(50);
image.setMaxWidth(50);
image.setImageDrawable(avatar);
theLayout.addView(image);
maybe I need to refresh the layout after I add the ImageView? How do you "refresh"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试以下代码,您将不需要刷新。将图像 url 放入
inputurl
变量中。Try the following code, and you will not need to refresh. Put your image url into the
inputurl
variable.我认为你必须定义副总裁的参数,
如果你定义参数,它将显示如下图像-
I think u have to deifne the parameters for the vp
if u will define parameters it will display the image like-
您可以使用另一个线程来下载图像,下载完成后,您可以使用处理程序更新可绘制对象。您可以在文档中了解如何创建另一个线程。这看起来并不容易,但如果你想构建响应更快的 Android 应用程序,最好学习如何做到这一点。
(标题)带有第二个线程的 ProgressDialog 示例
http://developer.android.com/guide/topics/ui/dialogs.html
You can use another thread to download the image, and after download finishes you can update the drawable by using a handler. You can see how to make another thread at the documentation. It does not seems easy but it would be better to learn how to do it, if you want to build more responsive android apps.
(title) Example ProgressDialog with a second thread
http://developer.android.com/guide/topics/ui/dialogs.html