Android:动态设置ImageView?

发布于 2024-11-01 17:56:21 字数 466 浏览 3 评论 0原文

我正在尝试下载图像,并动态创建 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

过去的过去 2024-11-08 17:56:21

尝试以下代码,您将不需要刷新。将图像 url 放入 inputurl 变量中。

InputStream is = null;
String inputurl = " Enter url of ur image ";
try {
        URL url = new URL(inputurl);
        Object content = url.getContent();
        is = (InputStream) content;
        avatar = Drawable.createFromStream(is,"src");
} catch (MalformedURLException e) {
        e.printStackTrace();
} catch (IOException e) {
        e.printStackTrace();
}

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);

Try the following code, and you will not need to refresh. Put your image url into the inputurl variable.

InputStream is = null;
String inputurl = " Enter url of ur image ";
try {
        URL url = new URL(inputurl);
        Object content = url.getContent();
        is = (InputStream) content;
        avatar = Drawable.createFromStream(is,"src");
} catch (MalformedURLException e) {
        e.printStackTrace();
} catch (IOException e) {
        e.printStackTrace();
}

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);
Hello爱情风 2024-11-08 17:56:21

我认为你必须定义副总裁的参数,

如果你定义参数,它将显示如下图像-

LinearLayout.LayoutParams Params=new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
        Params.setMargins(0,0,0,0);

        Params=new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);

I think u have to deifne the parameters for the vp

if u will define parameters it will display the image like-

LinearLayout.LayoutParams Params=new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
        Params.setMargins(0,0,0,0);

        Params=new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
岁月静好 2024-11-08 17:56:21

您可以使用另一个线程来下载图像,下载完成后,您可以使用处理程序更新可绘制对象。您可以在文档中了解如何创建另一个线程。这看起来并不容易,但如果你想构建响应更快的 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文